Skip to content

Instantly share code, notes, and snippets.

@mendelgusmao
Last active December 20, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mendelgusmao/6076361 to your computer and use it in GitHub Desktop.
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>
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