Skip to content

Instantly share code, notes, and snippets.

@nyov
Last active November 1, 2015 14:25
Show Gist options
  • Save nyov/248ac09eac7f2849bbff to your computer and use it in GitHub Desktop.
Save nyov/248ac09eac7f2849bbff to your computer and use it in GitHub Desktop.
Adblock using /etc/hosts (need no stupid browser plugin)
#!/bin/sh
#
# (c) 2013 "nyov"
set -e
#
# Script to download, update and append blocklist to /etc/hosts
# for more info, visit http://winhelp2002.mvps.org/hosts.htm
#
# Must run as root to edit /etc/hosts (dangerous)
# or allow /etc/hosts to be edited by some other group
# (e.g. chgrp adm /etc/hosts; adduser xyz adm)
#
# Best backup your hosts file before first run!
# Define local blackhole IP
# On a proxy server this may point to a webserver which
# serves a cheery "This page is blocked" message.
# As a 404 error page of course, so nothing breaks.
#
BLACKHOLE_IP='127.0.0.254'
# Magic lines between which the script will operate
# Add those two lines somewhere in your /etc/hosts
# before running the script.
# Otherwise nothing will happen.
# # echo -e '# [START OF SCRIPT-GENERATED BLOCKLIST]\n# [END OF SCRIPT-GENERATED BLOCKLIST]' >> /etc/hosts
#
MAGIC_HEADER='# [START OF SCRIPT-GENERATED BLOCKLIST]'
MAGIC_FOOTER='# [END OF SCRIPT-GENERATED BLOCKLIST]'
# Work in a temporary location
cd /tmp
hostsblock () {
# fetch the file, if newer
wget -N -nv "http://winhelp2002.mvps.org/hosts.txt"
# check return value
# (sadly doesnt differ whether we downloaded anything or not, with wget)
if [ ${?} != 0 ]; then
return ${?}
fi
# work on a copy (to not break update checking)
cp -a hosts.txt tmphosts
# needs a dos2unix conversion
dos2unix tmphosts
# alternatively...
#sed -i 's/^M$//' tmphosts
# remove file header
sed -i -e '/./{H;$!d;}' -e 'x;/Disclaimer/d;' tmphosts
# strip default localhost entries (we have our own!)
sed -i -e '/127\.0\.0\.1 localhost/d' -e '/::1/d' tmphosts
sed -i -e '/127\.0\.0\.1 localhost/d' -e '/::1/d' tmphosts
# convert 0.0.0.0 to local blackhole ip
sed -i -e "s/^0\.0\.0\.0/$BLACKHOLE_IP/" tmphosts
# and lastly remove all empty lines left
sed -i -e '/^$/d' -e '/./!d' tmphosts
# now clean a previous blocklist from /etc/hosts
#sed -i '/^# \[Start of entries generated by WinHelp2002\]/,/^# \[end of entries generated by WinHelp2002\]/d' /etc/hosts
#sed -i '/^# \[Start of entries generated by MVPS HOSTS\]/,/^# \[end of entries generated by MVPS HOSTS\]/d' /etc/hosts
# and add the new list at the end of it
#cat tmphosts >> /etc/hosts
# Safer solution, using our own magic header
# delete everything between those two lines
sed -i '/^# \[START OF SCRIPT-GENERATED BLOCKLIST\]/,/^# \[END OF SCRIPT-GENERATED BLOCKLIST\]/{/^# \[START/!{/^# \[END/!d}}' /etc/hosts
# and add the new list after the magic header
sed -i '/^# \[START OF SCRIPT-GENERATED BLOCKLIST\]/r tmphosts' /etc/hosts
rm tmphosts
# possibly remove the file to force fresh download
#rm hosts.txt
echo "Done"
}
hostsblock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment