<div dir="ltr"><div><div>A couple of years ago, I wanted to implement a network wide ad blocking scheme. Since I am using OpenWRT, I came up with the script below, based on some recipes online. <br><br></div><div>The whole idea is to add to dnsmasq a list of domains that are aliased to either 0.0.0.0 or 127.0.0.1. You then make the OpenWRT device your only DNS server. The script updates the list once a week.<br><br></div>For some reason, I never enabled it. Perhaps because uBlock Origin took care of ads on my laptop and my tablet. Or maybe because the list list is over 66,500 long and I was concerned that it would slow my router. I don't remember.<br><br></div>Maybe someone can find this useful. Please report back if you try it.<br><div><div><div><br><font size="1">#!/bin/sh                                                                  <br>                                                                           <br># For OpenWRT,                                                             <br># Grab and sort a list of adservers and malware hostnames, and block them            <br>                                                                                     <br># Install this file in /etc/custom/adblock.sh                                        <br># Add it to cron by pasting the following line to System -> Scheduled Tasks          <br># 0 4 * * 1 sh /etc/adblock.sh <br><br>URL_LIST="                                                                 <br><a href="http://pgl.yoyo.org/adservers/serverlist.php?showintro=1;hostformat=hosts;mimetype=plaintext">pgl.yoyo.org/adservers/serverlist.php?showintro=1;hostformat=hosts;mimetype=plaintext</a><br><a href="http://winhelp2002.mvps.org/hosts.txt">winhelp2002.mvps.org/hosts.txt</a>                                                       <br><a href="http://www.malwaredomainlist.com/hostslist/hosts.txt">www.malwaredomainlist.com/hostslist/hosts.txt</a>                                        <br><a href="http://hosts-file.net/ad_servers.txt">hosts-file.net/ad_servers.txt</a> "                                                      <br>                                                                                     <br>BLOCK_LIST=/etc/custom/block.hosts                                                   <br>                                                                                     <br>TMP_LIST=/tmp/block.list                                                             <br>                                             <br># Check proper DHCP config and, if necessary, update it<br>uci get dhcp.@dnsmasq[0].addnhosts > /dev/null 2>&1 || <br>  uci add_list dhcp.@dnsmasq[0].addnhosts=$BLOCK_LIST &&<br>  uci commit                                            <br>                                                        <br>CUSTOM_LIST=$(uci get dhcp.@dnsmasq[0].addnhosts)       <br>                                                        <br># Delete the old blocked hosts to make room for the updates<br>rm -f $BLOCK_LIST                                          <br>                                                           <br># Empty the temp file                                      <br>> $TMP_LIST                                                <br>                                                           <br># Download and process the files needed to make the lists  <br>for URL in "$URL_LIST"                                     <br>do                                                         <br>  wget -qO- "http://$URL" >> $TMP_LIST                   <br>done                                                     <br>                                                         <br># If we have a custom file, then include it too          <br>if [ -s "$CUSTOM_LIST" ]; then                            <br>  cat $CUSTOM_LIST >> $TMP_LIST                           <br>fi                                                        <br>                                                          <br># Sort the lists, and remove the Microsoft Carriage Return ^M<br>sort -u $TMP_LIST |                                          <br>  sed -e "s/\r//g" > $BLOCK_LIST                             <br>                                                             <br># Delete files used to build list to free up the limited space<br>rm -f $TMP_LIST                                               <br>                                                              <br># Tell dnsmasq to re-read its configuration                   <br>killall -HUP dnsmasq                                          <br>                                                              <br>exit 0</font><br></div></div></div></div>