Sync Address List from DNS Lookup Results - C and A Records

From MikroTik Wiki
Revision as of 11:41, 14 January 2012 by ElPablo (talk | contribs) (Created page with ":global ListName :global Servers :global Done #has $Done been initialized? :if ([:typeof $Done] != "boolean") do={ :set Done true; } #make sure previous runs have finished wh...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
global ListName
global Servers
global Done
  1. has $Done been initialized?
if ([:typeof $Done] != "boolean") do={
 :set Done true;

}

  1. make sure previous runs have finished

while (!$Done) do={

 :nothing;

}

  1. block any other runs
set Done false;
  1. delete old address lists
foreach aListItem in=[/ip firewall address-list find list=$ListName] do={
 /ip firewall address-list remove $aListItem;

}

foreach aServer in=$Servers do={
  1. force the dns entries to be cached
 :resolve $aServer;
 :foreach dnsRecord in=[/ip dns cache all find where (name=$aServer)] do={
  1. if it's an A records add it directly
   :if ([/ip dns cache all get $dnsRecord type]="A") do={
      /ip firewall address-list add list=$ListName address=[/ip dns cache all get $dnsRecord data] comment=$aServer;
   }
  1. if it's a CNAME follow it until we get A records
   :if ([/ip dns cache all get $dnsRecord type]="CNAME") do={
     :local cname;
     :local nextCname
     :set cname [/ip dns cache all find where (name=$aServer && type="CNAME")];
     :set nextCname [/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="CNAME")];
     :while ($nextCname != "") do={
         :set cname $nextCname;
         :set nextCname [/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="CNAME")];
       }
 
  1. add the a records we found
   :foreach aRecord in=[/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="A")] do={
     /ip firewall address-list add list=$ListName address=[/ip dns cache all get $aRecord data] comment=$aServer;
     }
   }
 }

}

  1. allow other scripts to call this
set Done true