Skip to content

Instantly share code, notes, and snippets.

@andreybpanfilov
Created November 7, 2016 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreybpanfilov/c3429b2fc583746aad527b033e3a6fc6 to your computer and use it in GitHub Desktop.
Save andreybpanfilov/c3429b2fc583746aad527b033e3a6fc6 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# /etc/NetworkManager/dispatcher.d/99-hosts
#
INTERFACE=$1
STATUS=$2
if [ "x$STATUS" != "xup" ]; then
exit 0
fi
DEFAULT=$(/sbin/ip route | grep ^default | awk '{print $5}')
if [ "x$INTERFACE" != "x$DEFAULT" ]; then
exit 0
fi
hostname=$(hostname)
alias=$(hotname -a)
ip=$(/sbin/ip -o -4 addr list $INTERFACE | awk '{print $4}' | cut -d/ -f1)
if [ "x$ip" = "x" -o "x$hostname" = "x" ]; then
exit 0
fi
sed -i -e '/'"$hostname"'/d' /etc/hosts
sed -i -e '/Added by 99-hosts/d' /etc/hosts
echo "#Added by 99-hosts" >> /etc/hosts
echo "$ip $hostname $alias" >> /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment