Created
June 11, 2013 15:27
-
-
Save anonymous/5757819 to your computer and use it in GitHub Desktop.
Update local hostname entry in /etc/hosts when an interface is brought up
This file contains 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 | |
if [[ "$1" == eth* ]]; then | |
echo "this part will be executed right after an interface is up." | |
# find out the ip | |
IP=$( ifconfig | grep "inet " | head -n 1 | awk '{print $2}' | awk 'BEGIN {FS = ":"} ; {print $2}' ) | |
# delete the existing lines in the file | |
FQDN=`hostname` | |
HOSTNAME=`hostname -s` | |
sed -i "/${HOSTNAME}/d" /etc/hosts | |
# add the correct line | |
echo "${IP} $HOSTNAME $FQDN" >> /etc/hosts | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment