If you’ve turned your $60 router into a user-friendly super-router with open-source firmware Tomato, you already know that Tomato can boost your Wi-Fi signal, track bandwidth usage, and set Quality of Service rules with ease. But if you’re a big fan of Adblock Plus—the most popular Firefox extension among Lifehacker readers—you can save yourself an extension installation and universally block ads across your entire home network by adding a custom script to your Tomato firmware. Here’s how it works:
There are several ad-blocking scripts available that could work, but the one I’m using comes from this forum post. Thanks Tad!
Installation
1. Open the Tomato Admin Scripts interface
You’ll need to login with the user name and password you set in our instructions for installing Tomato. Once you’re logged in, make sure to click on the WAN Up tab.
2. Copy the ad-block script to Tomato
I’d recommend going directly to the source, since the author of the script updates it regularly in the top post. As of this writing, the script looks like this:
#!/bin/sh
## Adblock script [Version 2.1 | 08 July 2008 | 3778 bytes]
##
## Created by Adrian Jon Kriel: root-AT-extremecooling-DOT-org
##
## tomato WAN Up script
##
## 0 = disable
## 1 = enable
## (1) = default value
## optimising of dnsmasq, (1)
eval OPTDNSMASQ=”1″
## automatic updating, (1)
eval AUTOUPDATE=”1″
## MVPS HOSTS ~18,500 lines, 680 Kbyte, (1)
eval MVPSSOURCE=”1″
## pgl.yoyo.org ~2,200 lines, 68 Kbyte, (1)
eval PGLSOURCE=”1″
## hosts-file.net ~53,000 lines, 1.5 Mbyte, (0)
eval HSFSOURCE=”0″
## Hosts File Project ~102,000 lines, 3.0 Mbyte ***6+mb free memory***, (0)
eval HFPSOURCE=”0″
##
## varibles
## location of temp file, (/tmp/hosts)
eval GENFILE=”/tmp/hosts”
## redirect ip, (0.0.0.0)
eval REDIRECTIP=”0.0.0.0″
## sources
eval MVPSOURCEFILE=”http://www.mvps.org/winhelp2002/hosts.txt”
eval PGLSOURCEFILE=”http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts”
eval HSFSOURCEFILE=”http://www.it-mate.co.uk/downloads/hosts.txt”
eval HFPSOURCEFILE=”http://hostsfile.mine.nu/Hosts”
if ping -c 1 yahoo.com ; then
eval GOTSOURCE=”0″
echo “” > $GENFILE
## download
if [ "$MVPSSOURCE" = "1" ] ; then
if wget $MVPSOURCEFILE -O - >> $GENFILE ; then
logger ADBLOCK Downloaded $MVPSOURCEFILE
eval GOTSOURCE=”1″
else
logger ADBLOCK Failed $MVPSOURCEFILE
fi
fi
if [ "$PGLSOURCE" = "1" ] ; then
if wget $PGLSOURCEFILE -O - >> $GENFILE ; then
logger ADBLOCK Load $PGLSOURCEFILE
eval GOTSOURCE=”1″
else
logger ADBLOCK Fail $PGLSOURCEFILE
fi
fi
if [ "$HSFSOURCE" = "1" ] ; then
if wget $HSFSOURCEFILE -O - >> $GENFILE ; then
logger ADBLOCK load $HSFSOURCEFILE
eval GOTSOURCE=”1″
else
logger ADBLOCK Fail $HSFSOURCEFILE
fi
fi
if [ "$HFPSOURCE" = "1" ] ; then
if wget $HFPSOURCEFILE -O - >> $GENFILE ; then
logger ADBLOCK Load $HFPSOURCEFILE
eval GOTSOURCE=”1″
else
logger ADBLOCK Fail $HFPSOURCEFILE
fi
fi
if [ "$GOTSOURCE" = "1" ]; then
logger ADBLOCK Got Source Files
#FREE MEMORY!
service dnsmasq stop
killall -9 dnsmasq
logger ADBLOCK Ignor Fail Safe
##strip source file
sed -i -e ‘/^[0-9A-Za-z]/!d’ $GENFILE
sed -i -e ‘/%/d’ $GENFILE
sed -i -e ’s/[[:cntrl:][:blank:]]//g’ $GENFILE
sed -i -e ’s/^[ t]*//;s/[ t]*$//’ $GENFILE
## dnsmasq, sanitize, optimised
sed -i -e ’s/[[:space:]]*[.*$//' $GENFILE
sed -i -e 's/[[:space:]]*].*$//’ $GENFILE
sed -i -e ‘/[[:space:]]*#.*$/ s/[[:space:]]*#.*$//’ $GENFILE
sed -i -e ‘/^$/d’ $GENFILE
sed -i -e ‘/127.0.0.1/ s/127.0.0.1//’ $GENFILE
sed -i -e ‘/^www[0-9]./ s/^www[0-9].//’ $GENFILE
sed -i -e ‘/^www./ s/^www.//’ $GENFILE
## remove duplicates (resource friendly)
cat $GENFILE | sort -u > $GENFILE.new
mv $GENFILE.new $GENFILE
## format
sed -i -e ’s|$|/’$REDIRECTIP’|’ $GENFILE
sed -i -e ’s|^|address=/|’ $GENFILE
## load values from dnsmasq config
cat /etc/dnsmasq.conf >> $GENFILE
## optimise dnsmasq
if [ "$OPTDNSMASQ" = "1" ] ; then
cat >> $GENFILE <<EOF
cache-size=2048
log-async=5
EOF
fi
## remove/whitelist websites
## removes 3 websites (aa.com, bb.com, cc.com)
## remove the # and edit the website urls.
sed -i -e ‘/aa.com/d’ $GENFILE
sed -i -e ‘/bb.com/d’ $GENFILE
sed -i -e ‘/cc.com/d’ $GENFILE
## apply blacklist
dnsmasq —conf-file=$GENFILE
## failsafe added
dnsmasq
logger ADBLOCK Ignor Fail Safe
## dev info
logger ADBLOCK Unique Hosts Blocked $(awk ‘END { print NR }’ $GENFILE)
else
logger ADBLOCK Error Not Downloaded
fi
else
logger ADBLOCK Error No Internet
fi
## remove the generated files
rm $GENFILE*
## automatic update
if [ "$AUTOUPDATE" = "1" ] ; then
## script exists
if [ -x /tmp/script_wanup.sh ] ; then
cru a UpdateAdlist “0 6,12,18,0 * * * /tmp/script_wanup.sh >/dev/null 2>&1″
fi
fi
## the end
You should paste the script into the blank text area in the WAN Up tab we navigated to above.
3. Save the script by clicking the Save button.

4. Reboot your router to enable the script.

That’s all there is to it. Next time you visit a web site, you should notice a conspicuous lack of ads. The same should be true from any computer, as long as its connected to your Tomato router.
There are some unfortunate bits and pieces about this method versus the Adblock extension that you might want to take into consideration before setting it up on your router. First, if you want to whitelist a site, you have to manually edit the script by changing the following section:
## remove/whitelist websites
## removes 3 websites (aa.com, bb.com, cc.com)
## remove the # and edit the website urls.
sed -i -e ‘/aa.com/d’ $GENFILE
…replacing aa.com with the site you wanted to whitelist. Keep in mind that whitelisting does not work for whitelisting all ads on a specific site—instead, if you’re having trouble opening a site you want to look at because it’s on a blacklist, whitelisting that site will let you access that one specific site. As someone who makes a living writing for an ad-supported web site, I normally encourage people to allow ads on sites they care about. Unfortunately the script doesn’t allow you to whitelist all ads on specific sites. (It may be possible, though, so if you know how to go about doing so, I’d love to hear it in the comments!)
I am aware that many of you rely on ad-blocking scripts or extensions to speed up your slow internet, (in fact, we’ve recommended that you do as much to survive a slow internet connection), and others of you just hate dealing with the eyesore that flashy ads can cause.
So while I still heartily encourage supporting sites you frequent, this simple script makes setting up a universal ad-block on your home network a breeze. If you’ve used a similar method to block ads across your network, let’s hear about it in the comments.



