#!/bin/bash # Hosts file updater # Originally written by Andy Short circa 2002 # You will probably need root or su rights to access /etc/hosts # modified by killermist with (what seem to be) some logical tweaks # 20090712 alfredo (1) Added checks for required applications; # (2) put 'footprint' text into the modified hosts file. # Debian users will need the package "sysutils" as this script uses dos2unix # $ sudo apt-get install sysutils # HFSERVER="http://hostsfile.mine.nu.nyud.net" HFILE="hosts.zip" ORIGFILE="/etc/hosts.original" clear echo "-------------------------------------------------------------" echo "This script will update your Hosts file to the latest version" echo "Your original Hosts file will be renamed to $ORIGFILE" echo "-------------------------------------------------------------" echo "" echo "Checking for required applications..."; ABORT=0 echo ... wget ; builtin type -P wget &>/dev/null || { echo "wget is missing."; ABORT=1; } echo ... unzip ; builtin type -P unzip &>/dev/null || { echo "unzip is missing."; ABORT=1; } echo ... dos2unix; builtin type -P dos2unix &>/dev/null || { echo "tofrodos (which contains dos2unix) is missing."; ABORT=1; } echo ... grep ; builtin type -P grep &>/dev/null || { echo "grep is missing."; ABORT=1; } # this is just to test the test! #echo ... crap ; builtin type -P crap &>/dev/null || { echo "crap is missing."; ABORT=1; } #echo ABORT is $ABORT if [ $ABORT != 0 ] ; then echo One or more required applications are missing. Aborting now ... exit 1 fi echo "OK" echo "" if [ ! -f "$ORIGFILE" ] ; then echo "Backing up your previous hosts file.." cp -v /etc/hosts $ORIGFILE # I like verbose file operations. Can be less verbose if necessary. fi echo "Retrieving $HFILE from $HFSERVER" echo "" wget -O /tmp/$HFILE $HFSERVER/$HFILE unzip -p /tmp/$HFILE | dos2unix > /tmp/hosts if [ 'grep -c "banner" /tmp/hosts' ];then echo "Downloaded and unpacked $HFILE OK" echo "Appending host list to original content" # which was probably there for a reason, like to make sure localhost worked, and possibly even more stuff if part of a corporate LAN #cp -f -u /tmp/hosts /etc/hosts cat $ORIGFILE >/etc/hosts echo "" >>/etc/hosts # to make sure the original file ends in a new-line so that 2 entries don't end up on the same line, either causing unexpected behavior or not working at all echo "#=============================================================" >>/etc/hosts echo "# This hosts file has been modified by the script:" >>/etc/hosts echo "# $0" >>/etc/hosts echo "# with a list of blocked sites obtained from $HFSERVER" >>/etc/hosts echo "# from the original file now saved as $ORIGFILE" >>/etc/hosts echo "# As a side-effect of this script, any changes you wish to make" >>/etc/hosts echo "# persistent in the hosts file should be made to $ORIGFILE" >>/etc/hosts echo "# because /etc/hosts will be respawned from that file and the" >>/etc/hosts echo "# new list from the server each time this script runs." >>/etc/hosts echo "#=============================================================" >>/etc/hosts cat /tmp/hosts >>/etc/hosts rm -fv /tmp/hosts* # again, I like verbose file operations. I like to know what my system is doing. echo "Update process complete" #echo "-------------------------------------------------------------" echo "As a side-effect of this script, any changes you wish to make" echo "persistent in the hosts file should be made to $ORIGFILE" echo "because /etc/hosts will be respawned from that file and the " echo "new list from the server each time this script runs." exit else echo "Update failed" fi