Block URLs with an Automatically Downloaded Host File
This was originally taken from mraneri from the Linksys forum, but was heavily modified. And thanks to DD-Wrt Wiki
#!/bin/sh
logger WAN up script executing
sleep 5
if test ! -s /tmp/dlhosts
then
cat >/tmp/dlhosts <<"EOF"
#!/bin/sh
logger Downloading http://www.mvps.org/winhelp2002/hosts.txt
wget -O - http://www.mvps.org/winhelp2002/hosts.txt |
grep 127.0.0.1 |
sed '2,$s/127.0.0.1/0.0.0.0/g; s/[[:space:]]*#.*$//g;' |
grep -v localhost |
tr ' ' '\t' |
tr -s '\t' |
sort -u >/tmp/hosts0
grep addn-hosts /tmp/dnsmasq.conf ||
echo "addn-hosts=/tmp/hosts0" >>/tmp/dnsmasq.conf
logger Restarting dnsmasq
killall -1 dnsmasq
EOF
chmod 777 /tmp/dlhosts
/tmp/dlhosts
fi
grep -q '/tmp/dlhosts' /tmp/crontab ||
echo "45 23 * * 5 root /tmp/dlhosts" >>/tmp/crontab
This script automatically downloads a host file from: "http://www.mvps.org/winhelp2002/hosts.txt" and redirects all the URLs in that file to 127.0.0.1. All those URLs are common malware or advertisement sites so is better to block them. You can also download the file, modify it with new URLs that you want to block or delete the ones you don't want to block and then upload to a web site and change the URL in the code to your custom one. Be aware that the more URLs in the file the more RAM that you will be eating from your router. Check the file size and your free memory to see if it will suit you. If not just erase some URLs... If you want to block all URLs since the router boots then just placed in the startup scripts.
Update by Aviad (A.K.A. Hotfortech): While the above works just fine, there are two main disadvantages of the above method:
1. The blocked content will be shown as "page cannot be displayed" within the websites advertisement segments and will cause the page to not load until the browser gives up on the missing object.
2. If you want to add sites to the block list, you have to do it on a per host bases... (tedious)
Enter shameless self promotion -> I have created a script on my wiki that deals with the above and more by using the pixelserv method described in the forum. you can find the script and a detailed explanation on how it works on my wiki: http://hotfortech.wikispaces.com/How+to+remove+advertisements+with+pixelserv+on+DD-WRT
Other Host Files
- MVPS HOSTS file: http://www.mvps.org/winhelp2002/hosts.txt (~18,500 records, 680 Kbyte)
- PGL YOYO: http://pgl.yoyo.org/adservers/server...stformat=hosts (~2,200 records, 68 Kbyte)
- hosts-file.net: http://www.it-mate.co.uk/downloads/hosts.txt (~53,000 records, 1.5 Mbyte)
- The Hosts File Project: http://hostsfile.mine.nu/Hosts (~102,000 records, 3.0 Mbyte) ***requires 6mb free memory***
Comments
Post a Comment