blockhosts.sh (Source)

#!/bin/sh
# block servers based on /etc/hosts
# Downloads a hosts-file with hostnames to be blocked.
# Takes the current hosts file and
# appends the downloaded hosts to the original.
src='http://hosts-file.net/download/hosts.zip'
dst='/tmp/downloaded-hosts-file'
wget $src --output-document $dst
# The lines before the marker comment
# are the user-created entries to be preserved.
marker='#---generated-content---'
sed -e '/'$marker'/,$d' < /etc/hosts > /tmp/hosts
echo $marker >> /tmp/hosts
echo "# Generated: `date`" >> /tmp/hosts
echo '' >> /tmp/hosts
# unzip options:
# -C case-insensitive (match "HOSTS" or "hosts")
# -p output to pipe
#
# egrep-filters
#  exclude comments
#  exclude standard entry `127.0.0.1   locahost`
#  allow only re-route to 127.0.0.1
unzip -Cp $dst | egrep -v '^#|^.*?localhost' | egrep '127.0.0.1' >> /tmp/hosts
rm $dst
echo "Complete. Updated hosts file is /tmp/hosts"
exit 0