Skip to content

Instantly share code, notes, and snippets.

@M-D-M
Last active October 7, 2016 10:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M-D-M/94dbc83aefdab631b37d to your computer and use it in GitHub Desktop.
Save M-D-M/94dbc83aefdab631b37d to your computer and use it in GitHub Desktop.
#!/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
@Temporary-3142
Copy link

Hello there Captain Skyhawk. I use your hosts file updater. Thanks for that. I found a bugette. dos2unix isn't present in Mint 17.3 so also probably not in Ubuntu etc. Just a FYI. All the best, Temporary Pi.

@M-D-M
Copy link
Author

M-D-M commented Aug 5, 2016

No problem! I figured dos2unix was easy enough to install.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment