Skip to content

Instantly share code, notes, and snippets.

@felipepodesta
Forked from srinivasmohan/update_hosts.sh
Created November 13, 2013 18:14
Show Gist options
  • Save felipepodesta/7453668 to your computer and use it in GitHub Desktop.
Save felipepodesta/7453668 to your computer and use it in GitHub Desktop.
#!/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