Skip to content

Instantly share code, notes, and snippets.

@mendelgusmao mendelgusmao/gist:6076361
Last active Dec 20, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.