Last active
December 20, 2015 05:09
-
-
Save mendelgusmao/6076361 to your computer and use it in GitHub Desktop.
updates /etc/hosts whenever a host ip changes
useful for creating host aliases
usage: update-hosts <host_alias> <host_ip>
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 characters
hosts=/etc/hosts | |
host=$1 | |
ip=$2 | |
if [ "$ip" = "" ]; then | |
ip=$(wget -qO- http://checkip.dyndns.org | cut -f2 -d":" | cut -f1 -d"<" | sed "s/ //") | |
fi | |
line=$(grep $host $hosts) | |
if [ $? -ne 0 ]; then | |
echo "$ip\t$host\t# $(date)" >> $hosts | |
exit 0 | |
fi | |
oldip=$(echo "$line" | awk '{print $1}') | |
if [ "$ip" != "$oldip" ]; then | |
sed -i "s/$line/$ip\t$host\t# $(date)/" $hosts | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment