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