Tuesday, March 31, 2015

Mikrotik Synchronize Address List

This is a unproblematic script solution to synchronize small-scale address lists betwixt Mikrotik routers. It is express past times the fact that at that spot is a 4096 byte boundary for variables inward Mikrotik Scripts. I convey maximized the pose out if entries you lot tin john sync past times putting solely the listing get upwards as well as address inward the file


On the host router add together the next script:

#Semicolon separated listing of Address Lists you lot desire to sync
:local lists {"List1";"List2"}

#Filename of export (must fit receiving router import filename
:local exportFile ExportAddressList

/file take [:file notice name=$exportFile]

:local ipAddress
:local listEntry
:local fileContents

:set fileContents ""

:foreach listName in=$lists do={
  :foreach listEntry in=[/ip firewall address-list notice where list=$listName] do={
    :set ipAddress [/ip firewall address-list dice $listEntry address]
    :log information "=$listName"
    :set fileContents "$fileContents$listName=$ipAddress\n"
    }
}
/file impress file=$exportFile
/file gear upwards $exportFile contents=$fileContents


On the customer router, add together the next script.
#Filename of export (must fit receiving router import filename
:local importFile ExportAddressList.txt

#IP Address of router amongst existing address list(s)
:local hostIP 1.2.3.4

#FTP Username as well as Password
:local ftpUser username
:local ftpPassword userpassword

/tool fetch address=$hostIP src-path=$importFile dst-path=$importFile mode=ftp user=$ftpUser password=$ftpPassword
:local fileContent [/file dice [/file notice name=$importFile] contents]

#IMPORT NEW ROUTES
{
#Declare Local Variables
    :local contentLength [:len $fileContent];
    :local lineEnd 0
    :local lineContent ""
    :local lastEnd 0
    :local addressList
    :local addressListEnd
    :local ipAddress
    :local ipAddressStart
    :local lineLength

    :while ($lineEnd < $contentLength) do={
        :set lineEnd [:find $fileContent "\n" $lastEnd]
        :if ([:len $lineEnd] = 0) do={
            :set lineEnd $contentLength
        }
        :set lineContent [:pick $fileContent $lastEnd $lineEnd]
        :set lastEnd ($lineEnd +1)
        : if ($lineContent !="") do={
            :set addressListEnd [:find $lineContent "=" -1]
            :set addressList [:pick $lineContent 0 $addressListEnd]
            :set lineLength [:len $lineContent]
            :set ipAddressStart ($addressListEnd+1)
            :set ipAddress [:pick $lineContent $ipAddressStart $lineLength]
            /ip firewall address-list
            :if ([/ip firewall address-list notice address=$ipAddress]="") do={add list=$addressList address=$ipAddress comment="Imported from $hostIP"}
        }
#^ENDIF^
    }
#^ENDWHILE^

}
#END OF IMPORT


#REMOVE ROUTES THAT ARE NO LONGER IN HOST LIST
{
    :local listEntry
    :local listName
    :local listIP
    :local listComment
    :local findResult
 
    :foreach listEntry in=[/ip firewall address-list find] do={
        :set listIP [/ip firewall address-list dice $listEntry address]
        :set listName [/ip firewall address-list dice $listEntry list]
        :set listComment [/ip firewall address-list dice $listEntry comment]
        :if ($listComment ="Imported from $hostIP") do={
            :set findResult [:find $fileContent "$listName=$listIP" -1]
            :set findResult "a$findResult"
            :if ($findResult ="a") do={
                /ip firewall address-list take $listEntry
            }
        }
    }
}
#END OF REMOVE ROUTES


Remember to supervene upon the variables amongst your values.
You tin john immediately gear upwards the scheduler to synchronize the lists at regular intervals.