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!

logger WAN up script executing
if test -s /tmp/hosts0
        rm /tmp/hosts0

logger Downloading http://www.mvps.org/winhelp2002/hosts.txt
wget -O - http://www.mvps.org/winhelp2002/hosts.txt | grep |
	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

cat dns_local.txt this that 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.