Skip to content

Instantly share code, notes, and snippets.

@jac18281828
Last active July 5, 2018 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jac18281828/31deef842ec0aa7a6f04bdc249da392f to your computer and use it in GitHub Desktop.
Save jac18281828/31deef842ec0aa7a6f04bdc249da392f to your computer and use it in GitHub Desktop.
Ad Blocking script for dnsmasq. This uses Steven Black's Ad Blocking /etc/hosts file but converts it to dnsmasq format. For use with either an AdBlocking Raspberry PI project (PiHole) or on a Unify EdgeRouter
import sys
for buffer in sys.stdin.read():
for ch in buffer:
if (ord(ch) < 128):
sys.stdout.write(ch)
#!/bin/bash
ad_hosts_url="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
# The IP address below should point to the IP where blocked traffic should go
# or to a non routable address such as 0.0.0.0
# OpenDNS block page
# pixelserv_ip="146.112.61.104"
# not routable address
pixelserv_ip="0.0.0.0"
ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf"
temp_ad_file="/tmp/dnsmasq.adlist.conf.tmp"
#cat hosts | \
curl -sk ${ad_hosts_url} | \
python filter_unicode.py | \
sed "/^\s*\#/d" - | \
sed "/^\s*\$/d" - > ${temp_ad_file}
if [ -f "$temp_ad_file" ]
then
# blacklist sites
cat << EOF >> ${temp_ad_file}
0.0.0.0 dclk-match.dotomi.com
0.0.0.0 cdn.spotxcdn.com
0.0.0.0 login-ds.dotomi.com
EOF
# convert to dnsmasq
cat ${temp_ad_file} | \
sed '/whitelist-domain-here\.com/d' - | \
awk '$1 == "0.0.0.0" { print "address=/" $2 "/" $1 "/" }' - | \
sort | uniq > ${ad_file}
rm -f ${temp_ad_file}
else
echo "Error building the ad list, please try again."
exit
fi
/etc/init.d/dnsmasq force-reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment