Created
November 7, 2016 01:17
-
-
Save andreybpanfilov/c3429b2fc583746aad527b033e3a6fc6 to your computer and use it in GitHub Desktop.
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/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