Created
April 26, 2012 22:16
-
-
Save srinivasmohan/2503614 to your computer and use it in GitHub Desktop.
Update /etc/hosts to have the "current" IP address of eth0
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
#!/bin/bash | |
#/etc/network/if-up.d/update_hosts | |
set -e | |
#Variable IFACE is setup by Ubuntu network init scripts to whichever interface changed status. | |
[ "$IFACE" == "eth0" ] || exit | |
myname=`cat /etc/hostname` | |
shortname=`cat /etc/hostname | cut -d "." -f1` | |
hostsfile="/etc/hosts" | |
#Knock out line with "old" IP | |
sed -i '/ '$myname'/ d' $hostsfile | |
ipaddr=$(ifconfig eth0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}') | |
echo "#Updated at `date`" >> $hostsfile | |
echo "$ipaddr $myname $shortname" >> $hostsfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment