Revisions
-
stvvt revised this gist
May 14, 2016 . 1 changed file with 29 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,41 +1,43 @@ #!/usr/bin/env bash set -eu # PATH TO YOUR HOSTS FILE : ${ETC_HOSTS="/etc/hosts"} # DEFAULT IP FOR HOSTNAME DEFAULT_IP="127.0.0.1" VERBOSE=false function remove() { local HOSTNAME=$1 local HOST_REGEX="\(\s\+\)${HOSTNAME}\s*$" local HOST_LINE="$(grep -e "${HOST_REGEX}" ${ETC_HOSTS})" if [ -n "${HOST_LINE}" ]; then [ ${VERBOSE} == true ] && echo "${HOSTNAME} Found in your ${ETC_HOSTS}, Removing now..." sed -i -e "s/${HOST_REGEX}/\1/g" -e "/^[^#][0-9\.]\+\s\+$/d" ${ETC_HOSTS} else [ ${VERBOSE} == true ] && echo "${HOSTNAME} was not found in your ${ETC_HOSTS}"; fi } function add() { local HOSTNAME=$1 local IP=${2:-${DEFAULT_IP}} local HOST_REGEX="\(\s\+\)${HOSTNAME}\s*$" local HOST_LINE="$(grep -e "${HOST_REGEX}" ${ETC_HOSTS})" if [ -n "${HOST_LINE}" ]; then [ ${VERBOSE} == true ] && echo "${HOSTNAME} already exists : ${HOST_LINE}" else [ ${VERBOSE} == true ] && echo "Adding ${HOSTNAME} to your ${ETC_HOSTS}"; echo -e "${IP}\t${HOSTNAME}" >> ${ETC_HOSTS} [ ${VERBOSE} == true ] && echo -e "${HOSTNAME} was added succesfully \n ${HOST_LINE}"; fi } # Execute $@ -
stvvt revised this gist
May 14, 2016 . 1 changed file with 10 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,21 +1,23 @@ #!/usr/bin/env bash # PATH TO YOUR HOSTS FILE : ${ETC_HOSTS="/etc/hosts"} # DEFAULT IP FOR HOSTNAME : ${IP="127.0.0.1"} # Hostname to add/remove. HOSTNAME=$1 VERBOSE=false function removehost() { if [ -n "$(grep $HOSTNAME /etc/hosts)" ] then [ ${VERBOSE} == true ] && echo "$HOSTNAME Found in your $ETC_HOSTS, Removing now..." sudo sed -i".bak" "/$HOSTNAME/d" $ETC_HOSTS else [ ${VERBOSE} == true ] && echo "$HOSTNAME was not found in your $ETC_HOSTS"; fi } @@ -24,16 +26,16 @@ function addhost() { HOSTS_LINE="$IP\t$HOSTNAME" if [ -n "$(grep $HOSTNAME /etc/hosts)" ] then [ ${VERBOSE} == true ] && echo "$HOSTNAME already exists : $(grep $HOSTNAME $ETC_HOSTS)" else echo "Adding $HOSTNAME to your $ETC_HOSTS"; sudo -- sh -c -e "echo '$HOSTS_LINE' >> /etc/hosts"; if [ -n "$(grep $HOSTNAME /etc/hosts)" ] then [ ${VERBOSE} == true ] && echo "$HOSTNAME was added succesfully \n $(grep $HOSTNAME /etc/hosts)"; else [ ${VERBOSE} == true ] && echo "Failed to Add $HOSTNAME, Try again!"; fi fi } -
irazasyed created this gist
Mar 7, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ #!/bin/sh # PATH TO YOUR HOSTS FILE ETC_HOSTS=/etc/hosts # DEFAULT IP FOR HOSTNAME IP="127.0.0.1" # Hostname to add/remove. HOSTNAME=$1 function removehost() { if [ -n "$(grep $HOSTNAME /etc/hosts)" ] then echo "$HOSTNAME Found in your $ETC_HOSTS, Removing now..."; sudo sed -i".bak" "/$HOSTNAME/d" $ETC_HOSTS else echo "$HOSTNAME was not found in your $ETC_HOSTS"; fi } function addhost() { HOSTNAME=$1 HOSTS_LINE="$IP\t$HOSTNAME" if [ -n "$(grep $HOSTNAME /etc/hosts)" ] then echo "$HOSTNAME already exists : $(grep $HOSTNAME $ETC_HOSTS)" else echo "Adding $HOSTNAME to your $ETC_HOSTS"; sudo -- sh -c -e "echo '$HOSTS_LINE' >> /etc/hosts"; if [ -n "$(grep $HOSTNAME /etc/hosts)" ] then echo "$HOSTNAME was added succesfully \n $(grep $HOSTNAME /etc/hosts)"; else echo "Failed to Add $HOSTNAME, Try again!"; fi fi }