DNS Blacklist on WRT

Recently I found a forum entry with a script for blocking ad’s via DNS for WRT (or any other OS using sh and dnsmasq). However it was missing two features: a white list and a list for your local hostnames. One can have multiple devices like servers or printers or IOT devices in his home, therefore you need local entries. All I did is add these additional features to the original script. Enjoy!

#!/bin/sh
logger WAN up script executing
if test -s /tmp/hosts0
then
        rm /tmp/hosts0
fi

logger Downloading http://www.mvps.org/winhelp2002/hosts.txt
wget -O - http://www.mvps.org/winhelp2002/hosts.txt | grep 0.0.0.0 |
	sed 's/[[:space:]]*#.*$//g;' |
	grep -v localhost | tr ' ' '\t' |
	tr -s '\t' | tr -d '\015' | sort -u >/tmp/hosts0
grep addn-hosts /tmp/dnsmasq.conf ||
	echo "addn-hosts=/tmp/hosts0" >>/tmp/dnsmasq.conf

## local list
while read line; do echo $line >>/tmp/hosts0; done < /tmp/dns_local.txt
## white list
while read line; do sed -i "/$line/d" /tmp/hosts0; done < /tmp/dns_whitelist.txt

logger Restarting dnsmasq
killall dnsmasq
dnsmasq --conf-file=/tmp/dnsmasq.conf

cat dns_whitelist.txt
www.tkqlhce.com
zr0.net

cat dns_local.txt
172.16.41.20 this
172.16.42.21 that
172.16.45.22 theotherthing

If you see room for improvement leave a comment below. Big thanks goes out to the author of the original script.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.