Last active
October 7, 2016 10:37
-
-
Save M-D-M/94dbc83aefdab631b37d 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/bash | |
remote_hosts_file="http://winhelp2002.mvps.org/hosts.txt" | |
hosts_file_loc="/etc/hosts" | |
main() { | |
printf "Downloading file...\n" | |
wget -q -O /tmp/hosts.new $remote_hosts_file | |
printf "Removing windows line breaks...\n" | |
dos2unix -q /tmp/hosts.new /tmp/hosts.new | |
printf "Removing commented lines...\n" | |
egrep -v '^#' /tmp/hosts.new > /tmp/hosts.tmp | |
printf "Removing lines before hosts entries...\n" | |
sed -n -i '/0\.0\.0\.0 /,$p' /tmp/hosts.tmp | |
printf "Performing malice check to see if anything points to anything other than 0.0.0.0...\n" | |
if [[ $(awk '{print $1}' /tmp/hosts.tmp | uniq) != '0.0.0.0' ]]; then | |
printf "File has been tampered with! Exiting!\n" | |
exit 1 | |
fi | |
# Replace 0.0.0.0 with 255.255.255.0 | |
# sed -i -e 's/0.0.0.0/255.255.255.0/g' /tmp/hosts.tmp | |
# Copy user's current hosts to ${hosts_file_loc}.initial | |
# (if initial run of this script) | |
if [ ! -r ${hosts_file_loc}.initial ]; then | |
cp ${hosts_file_loc} ${hosts_file_loc}.initial | |
fi | |
printf "Current hosts file: $(wc -l ${hosts_file_loc} | awk '{print $1}') lines...\n" | |
printf "Creating new ${hosts_file_loc} file with user's old hosts file and entries from new one...\n" | |
cat ${hosts_file_loc}.initial > ${hosts_file_loc} | |
printf "\n\n# Added on $(date)\n" >> ${hosts_file_loc} | |
cat /tmp/hosts.tmp >> ${hosts_file_loc} | |
printf "New hosts file: $(wc -l ${hosts_file_loc} | awk '{print $1}') lines...\n" | |
printf "Removing tmp files...\n" | |
rm /tmp/hosts* | |
} | |
main | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No problem! I figured dos2unix was easy enough to install.