Skip to content

Instantly share code, notes, and snippets.

@Pliner
Last active November 6, 2024 20:09
Show Gist options
  • Save Pliner/eb368a0836a11a49ae99bc29c195ad0d to your computer and use it in GitHub Desktop.
Save Pliner/eb368a0836a11a49ae99bc29c195ad0d to your computer and use it in GitHub Desktop.
Mikrotik antifilter.download update script
:do {
:do {
/file remove "/ipsum.rsc";
/file remove "/subnet.rsc";
} on-error={}
:put "Downloading ipsum.rsc...";
:do {
/tool fetch url="https://antifilter.download/list/ipsum.rsc" dst-path="/ipsum.rsc"
} on-error={
:put "Error. Download failed";
}
:put "Downloading subnet.rsc...";
:do {
/tool fetch url="https://antifilter.download/list/subnet.rsc" dst-path="/subnet.rsc"
} on-error={
:put "Error. Download failed";
}
/ip firewall address-list remove [/ip firewall address-list find list=rkn]
:put "Importing ipsum.rsc...";
:do {
/import "/ipsum.rsc";
} on-error={
:put "import failed. unknown error.";
}
:put "Importing subnet.rsc...";
:do {
/import "/subnet.rsc";
} on-error={
:put "import failed. unknown error.";
}
:put "Update Complete.";
}
@mms101
Copy link

mms101 commented Sep 25, 2019

/system script remove [find name=Antifilter]
/system script add name=Antifilter owner=admin policy=
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="{\r
\n\r
\n :local FileName "allyouneed.rsc";\r
\n :do {\r
\n :do {\r
\n /file remove "/$FileName";\r
\n } on-error={}\r
\n\r
\n :put "Downloading $FileName...";\r
\n :log warning "Downloading $FileName...";\r
\n :do {\r
\n /tool fetch url="https://antifilter.download/list/\$FileName\" dst-path="/$FileName"\r
\n } on-error={\r
\n :put "Error. Download $FileName failed";\r
\n :log error "Error. Download $FileName failed";\r
\n :return 0;\r
\n }\r
\n\r
\n /ip firewall address-list remove [/ip firewall address-list find list=rkn]\r
\n\r
\n :put "Importing $FileName...";\r
\n :log warning "Importing $FileName...";\r
\n :do {\r
\n /import "/$FileName";\r
\n } on-error={\r
\n :put "Import $FileName failed. unknown error.";\r
\n :log error "Import $FileName failed. unknown error.";\r
\n }\r
\n\r
\n :put "Update rkn address-list completed.";\r
\n :log warning "Update rkn address-list completed.";\r
\n }\r
\n}"

/system scheduler add name=Antifilter comment=Antifilter on-event=Antifilter interval=24h start-time=03:00:00

@zavrazhny
Copy link

/tool fetch url="https://antifilter.download/list/$FileName" dst-path="/$FileName"

выше надо убрать обратный слеш, обрамляющий $FileName

@NeoBeZ
Copy link

NeoBeZ commented Dec 14, 2019

:do {
:do {
/file remove "/allyouneed.rsc";
} on-error={}

:put "Downloading allyouneed.rsc...";
:do {
/tool fetch url="https://antifilter.download/list/allyouneed.rsc" dst-path="/allyouneed.rsc"
} on-error={
    :put "Error. Download failed";
}
   
/ip firewall address-list remove [/ip firewall address-list find list=rkn] 

:put "Importing allyouneed.rsc...";
:do {
    /import "/allyouneed.rsc";
} on-error={
    :put "import failed. unknown error.";
}

:put "Update Complete.";

}

@NeoBeZ
Copy link

NeoBeZ commented Dec 14, 2019

наворотили то

@ailinykh
Copy link

ailinykh commented Oct 2, 2021

:do {
    :do {
        /file remove "/allyouneed.rsc";
    } on-error={}

    :put "Downloading allyouneed.rsc...";
    :do {
	/tool fetch url="https://antifilter.download/list/allyouneed.rsc" dst-path="/allyouneed.rsc"
    } on-error={
        :put "Error. Download failed";
    }
	
    /ip firewall address-list remove [/ip firewall address-list find list=rkn]	
	
    :put "Importing allyouneed.rsc...";
    :do {
        /import "/allyouneed.rsc";
    } on-error={
        :put "import failed. unknown error.";
    }

    :put "Update Complete.";
}

@KPEBETKA
Copy link

Более безопасный вариант

:do {

    :local retryflag true;
    :local maxretry 3;
    :local delay 120s;
    :local url "https://antifilter.download/list/allyouneed.lst";
    :local listname "list-antifilter";

    :for retry from=1 to=$maxretry step=1 do={

        :if (retryflag) do={

            :set $retryflag false;
            :set $counter 0;

            :if (retry > 1) do={
                :delay $delay;
            };

            :do {
                /ip firewall address-list remove [find where list=($listname."-updated")];
            } on-error={};

            :do {
                /ip firewall address-list add list=($listname."-updated") address=antifilter.download comment="antifilter.download";
            } on-error={};

            :local filesize ([/tool fetch url=$url keep-result=no as-value]->"total");
            :local chunksize 64000;
            :local start 0;
            :local end ($chunksize - 1);
            :local chunks ($filesize / ($chunksize / 1024));
            :local lastchunk ($filesize % ($chunksize / 1024));

            :if ($lastchunk > 0) do={
                :set $chunks ($chunks + 1);
            };

            :for chunk from=1 to=$chunks step=1 do={

                :local comparesize ([/tool fetch url=$url keep-result=no as-value]->"total");

                :if ($comparesize = $filesize) do={
                    :set $data ([:tool fetch url=$url http-header-field="Range: bytes=$start-$end" output=user as-value]->"data");
                } else={
                    :set $data [:toarray ""];
                    :set $retryflag true;
                };

                :local regexp "^((25[0-5]|(2[0-4]|[01]?[0-9]?)[0-9])\\.){3}(25[0-5]|(2[0-4]|[01]?[0-9]?)[0-9])(\\/(3[0-2]|[0-2]?[0-9])){0,1}\$";

                :if ($start > 0) do={
                    :set $data [:pick $data ([:find $data "\n"]+1) [:len $data]];
                };
                
                :while ([:len $data]!=0) do={
                    :local line [:pick $data 0 [:find $data "\n"]];

                    :if ( $line ~ $regexp ) do={    
                        :do {
                            /ip firewall address-list add list=($listname."-updated") address=$line;
                            :set $counter ($counter + 1);
                        } on-error={};        
                    };

                    :set $data [:pick $data ([:find $data "\n"]+1) [:len $data]];

                    :if ([:len $data] < 256) do={
                        :set $data [:toarray ""];
                    };
                };

                :set $start (($start-512) + $chunksize); 
                :set $end (($end-512) + $chunksize); 
            
            };
        
        };

    };

    :if ($counter > 0) do={
        :do {
            /ip firewall address-list remove [find where list=$listname];
        } on-error={};

        :do {
            :foreach address in=[/ip firewall address-list find list=($listname."-updated")] do={
                :do {
                    /ip firewall address-list set list=$listname $address;
                } on-error={};
            };
        } on-error={};
    };

} on-error={};

Идея позаимствована тут

@mmaxs
Copy link

mmaxs commented Nov 13, 2021

@KPEBETKA, не плохо.

@To-Azamat
Copy link

:foreach address in=[/ip firewall address-list find list=($listname."-updated")] do={

Don't you miss a WHERE argument here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment