Last active
August 29, 2015 14:09
-
-
Save bkanuka/814ef1c9c1275e85745d to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
HEADER=" | |
127.0.0.1 localhost | |
# The following lines are desirable for IPv6 capable hosts | |
::1 ip6-localhost ip6-loopback | |
fe00::0 ip6-localnet | |
ff00::0 ip6-mcastprefix | |
ff02::1 ip6-allnodes | |
ff02::2 ip6-allrouters | |
" | |
TEMPHOSTS=$( tempfile -p hosts ) | |
for f in "$@"; do | |
grep -Eo '^[^#]*$' "$f" |\ | |
sed -e 's/^[ \t]\+//' |\ | |
sed -e 's/[ \t]\+$//' |\ | |
grep -Eo '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\s+(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$' |\ | |
awk '{print $1, $2}' |\ | |
sort >> $TEMPHOSTS | |
done | |
FINAL=$( tempfile -p hosts ) | |
echo 127.0.1.1 $(hostname) > $FINAL | |
cat >> $FINAL <<EOL | |
$HEADER | |
EOL | |
echo '' >> $FINAL | |
sort "$TEMPHOSTS" | uniq >> $FINAL | |
sudo cp $FINAL /etc/hosts | |
sudo chmod 644 /etc/hosts | |
rm $TEMPHOSTS $FINAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment