Block Adds With MikroTik

Ionut Micu created a cool MikroTik script to block adds by assigning 127.0.0.1 to their hostname, similar to Pihole. This is basically the same I just included a few allow list that I usually use, so I can come back here and copy&paste it whenever I need it.

WARNING, the script uses more than 100MB of RAM, do not run this if you don’t have at least 256RAM on your device!

:log warning "starting adblock update";
:delay 5;
:log warning "Downloading adblock from micu.eu...";
:local hostScriptUrl "https://www.micu.eu/adblock/adblock.php";
:local scriptName "adblock";

do {
    /tool fetch mode=http url=$hostScriptUrl dst-path=("./".$scriptName);
    :delay 20;

    :if ([:len [/file find name=$scriptName]] > 0) do={
        :log warning "removing old adblock list";
        /ip dns static remove [/ip dns static find address=127.0.0.1];
        :log warning "importing new adblock list";
        /import file-name=$scriptName;
        /file remove $scriptName;
        :log warning "adblock list imported";
        :log warning "Applying allow list...";
	/ip dns static remove [/ip dns static find name=googleadservices.com]
	/ip dns static remove [/ip dns static find name=www.googleadservices.com]
	/ip dns static remove [/ip dns static find name=doubleclick.net]
	/ip dns static remove [/ip dns static find name=ad.doubleclick.net]
	/ip dns static remove [/ip dns static find name=tracker.marinsm.com]
	/ip dns static remove [/ip dns static find name=pi.pardot.com]
	/ip dns static remove [/ip dns static find name=link.omaze.com]
	/ip dns static remove [/ip dns static find name=cb.sailthru.com]
	/ip dns static remove [/ip dns static find name=clickserve.dartsearch.net]
	/ip dns static remove [/ip dns static find name=aax-us-east.amazon-adsystem.com]
        :log warning "Allow list applied";
     } else={
        :log warning "adblock list not downloaded, script stopped";
        }
} on-error={
    :log warning "Adblock list download FAILED!";
};

This is how you run the script, on my RB760iGS it took ~4 minutes to complete. As you can see the cpu-load went up during the run. Upon completion dropped to normal load.

[user@RB760iGS] > system script run addblock2 
      status: finished
  downloaded: 850KiB
       total: 0KiB
    duration: 3s


Script file loaded and executed successfully
[user@RB760iGS] > system resource print interval=1
                   uptime: 3w2d23h15m8s
                  version: 6.48.1 (stable)
               build-time: Feb/03/2021 10:54:22
         factory-software: 6.44
              free-memory: 147.1MiB
             total-memory: 256.0MiB
                      cpu: MIPS 1004Kc V2.15
                cpu-count: 4
            cpu-frequency: 880MHz
                 cpu-load: 25%
           free-hdd-space: 2192.0KiB
          total-hdd-space: 16.3MiB
  write-sect-since-reboot: 1634001
         write-sect-total: 34237843
               bad-blocks: 0%
        architecture-name: mmips
               board-name: hEX S
                 platform: MikroTik

Remark: most webpages are using adds to sustain their business, therefore they are necessary. Above is just a possibility and it is discouraged.

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.