Created
March 7, 2015 09:16
-
-
Save irazasyed/a7b0a079e7727a4315b9 to your computer and use it in GitHub Desktop.
Bash Script to Manage /etc/hosts file for adding/removing hostnames.
This file contains 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 characters
#!/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 | |
} |
Productionized for my use of httperf. Tested a bit.
#!/bin/bash
# Synopsis:
# ./alterhost.sh add-host test.com:1.2.3.4
# ./alterhost.sh add-host 'test.com:1.2.3.4;test2.com:2.3.4.5;test3.com:3.4.5.6'
# ./alterhost.sh del-host test.com:1.2.3.4
# ALTER_VERBOSE=t ./alterhost.sh del-host test.com:1.2.3.4
if test $# -lt 2 ; then
echo "USAGE: alterhost <verb> '<name:ip;name:ip;name:ip...>' "
echo 'EX: ./alterhost.sh add-host test.com:1.2.3.4'
echo "EX: ./alterhost.sh add-host 'test.com:1.2.3.4;test2.com:2.3.4.5;test3.com:3.4.5.6'"
echo 'EX: ./alterhost.sh del-host test.com:1.2.3.4'
echo 'EX: ALTER_VERBOSE=t ./alterhost.sh del-host test.com:1.2.3.4'
echo "EX: ./alterhost.sh add-host 'www.test.org:2.3.5.6' ; httperf --server www.test.org --uri /LICENSE.txt --num-conns 10 --rate 10 --timeout 10 --hog --ssl ; ./alterhost.sh del-host 'www.test.org:2.3.5.6'"
exit -1
fi
# PATH TO YOUR HOSTS FILE
#ETC_HOSTS=/etc/hosts
ETC_HOSTS=./test_etc_hosts
[ ! -f $ETC_HOSTS.bak2 ] && cp -p $ETC_HOSTS $ETC_HOSTS.bak2
function msg() {
MESSAGE=$1
if [[ -z "${ALTER_VERBOSE}" ]]; then
exit 0
else
echo $MESSAGE
fi
}
function del-host() {
for i in $(echo $1 | sed -e 's/;/\n/g'); do
readarray -t HOSTIP < <(echo $i | awk -F':' '{print $1; print $2}')
IP=${HOSTIP[1]}
HOSTNAME=${HOSTIP[0]}
HOSTS_LINE="$IP[[:space:]]$HOSTNAME"
if [ -n "$(grep -P $HOSTS_LINE $ETC_HOSTS)" ]
then
msg "${HOSTIP[*]} Found in your $ETC_HOSTS, Removing now..."
sudo sed -i".bak" "/$HOSTS_LINE/d" $ETC_HOSTS
else
msg "${HOSTIP[*]} was not found in your $ETC_HOSTS"
fi
done
}
function add-host() {
for i in $(echo $1 | sed -e 's/;/\n/g'); do
readarray -t HOSTIP < <(echo $i | awk -F':' '{print $1; print $2}')
IP=${HOSTIP[1]}
HOSTNAME=${HOSTIP[0]}
HOSTS_LINE="$IP[[:space:]]$HOSTNAME"
line_content=$( printf "%s\t%s\n" "$IP" "$HOSTNAME" )
if [ -n "$(grep -P $HOSTS_LINE $ETC_HOSTS)" ]
then
msg "$line_content already exists : $(grep $HOSTNAME $ETC_HOSTS)"
else
msg "Adding $line_content to your $ETC_HOSTS";
sudo -- sh -c -e "echo '$line_content' >> $ETC_HOSTS";
if [ -n "$(grep -P $HOSTNAME $ETC_HOSTS)" ]
then
msg "$line_content was added succesfully";
else
msg "Failed to Add $line_content, better take a look!";
exit -1
fi
fi
done
}
$@
@kucerarichard in your script I needed to change the msg function from 'exit 0' to 'return' so that it wouldn't exit unless run with verbose flag.
function msg() {
MESSAGE=$1
if [[ -z "${ALTER_VERBOSE}" ]]; then
exit 0
else
echo $MESSAGE
fi
}
To:
function msg() {
MESSAGE=$1
if [[ -z "${ALTER_VERBOSE}" ]]; then
return
else
echo $MESSAGE
fi
}
What would be the downside/danger of looking at grep
's exit code instead of comparing strings?
if grep -q $HOSTNAME /etc/hosts; then
# Entry already exists
else
# Add in the entry
fi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this script does't remove, we need that. thanks!
For example I can't use experimental podman on an old host, so I have to build and use httperf directly and try to add host aliases without being too cumbersome:
add_host_alias "host:ip" && httperf ... && remove_host_alias "host:ip"