Skip to content

Instantly share code, notes, and snippets.

@jamespfinn
Created May 1, 2013 13:12
Show Gist options
  • Save jamespfinn/5495209 to your computer and use it in GitHub Desktop.
Save jamespfinn/5495209 to your computer and use it in GitHub Desktop.
simple script for blocking adds. add it to cron to keep your hosts file up to date. seems to work very well
#!/bin/bash
#
# 05.31.12 JamesPFinn aT gmail DoT cOm
#
# Script meant to be run from cron will update hosts file
# with contents from http://someonewhocares.org/hosts/hosts
# the contents will serve to block ads and other undesirables.
#
# Do not make customizations to the host file between:
# #JFADBLOCKSTART
# AND
# #JFADBLOCKSTOP
# They will be removed on the next run of this script.
#
# Backup hosts file
#
sudo cp /etc/hosts{,.bak}
#
# Check if we have already updated the hosts file before
#
( grep "^#JFADBLOCKSTART" /etc/hosts |:) && PREMOD="TRUE" || PREMOD="FALSE"
if [ $PREMOD == "TRUE" ] ; then
head -$((`grep -n "^#JFADBLOCKSTART" /etc/hosts | cut -d\: -f1`-1)) /etc/hosts > /tmp/hosts.tmp
tail -$((`wc -l /etc/hosts| cut -d" " -f1` - `grep -n "^#JFADBLOCKSTOP" /etc/hosts | cut -d\: -f1`)) /etc/hosts >> /tmp/hosts.tmp
else
cp /etc/hosts /tmp/hosts.tmp
fi
#
# Block the ads!
#
echo "#JFADBLOCKSTART" >> /tmp/hosts.tmp
curl -s http://someonewhocares.org/hosts/hosts | egrep -v '^#|localhost|broadcasthost|^$' >> /tmp/hosts.tmp
echo "#JFADBLOCKSTOP" >> /tmp/hosts.tmp
sudo cp /tmp/hosts.tmp /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment