Difference between revisions of "Using Fetch and Scripting to add IP Address Lists"

From MikroTik Wiki
Jump to: navigation, search
(The Code)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
Note: This only works with files under 4096 characters in size due to the variable size limit in v3
 +
hopefully they will re-introduce the LUA system in v4 shortly so we can make this work with any size list.
 +
 +
If not I will be investigating an alternate solution for breaking files up into readable chunks.
 +
 +
Regards,
 +
Andrew Cox
 +
 
==The Code==
 
==The Code==
  
Line 22: Line 30:
 
           :if ( [:pick $line 0 1] != "#" ) do={
 
           :if ( [:pick $line 0 1] != "#" ) do={
 
      
 
      
         :local entry [:pick $line 0 ($lineEnd -1) ]
+
         :local entry [:pick $line 0 $lineEnd ]
 
         :if ( [:len $entry ] > 0 ) do={
 
         :if ( [:len $entry ] > 0 ) do={
 
             /ip firewall address-list add list=MY-IP-LIST address=$entry
 
             /ip firewall address-list add list=MY-IP-LIST address=$entry
Line 29: Line 37:
 
     } while ($lineEnd < $contentLen)
 
     } while ($lineEnd < $contentLen)
 
     }
 
     }
 
  
 
==How to use==
 
==How to use==
Line 53: Line 60:
 
     #Replace with whatever name you have called the processing script
 
     #Replace with whatever name you have called the processing script
 
     /system script run add-ip-addresses
 
     /system script run add-ip-addresses
 +
 +
[[Category:Scripting]]

Latest revision as of 11:21, 30 July 2018

Note: This only works with files under 4096 characters in size due to the variable size limit in v3 hopefully they will re-introduce the LUA system in v4 shortly so we can make this work with any size list.

If not I will be investigating an alternate solution for breaking files up into readable chunks.

Regards, Andrew Cox

The Code

   ## Generic IP address list input
   ## Based on a script written by Sam Norris, ChangeIP.com 2008
   ## Edited by Andrew Cox, AccessPlus.com.au 2008
   :if ( [/file get [/file find name=ipaddress.txt] size] > 0 ) do={
   # Remove exisiting addresses from the current Address list
   /ip firewall address-list remove [/ip firewall address-list find list=MY-IP-LIST]
   
   :global content [/file get [/file find name=ipaddress.txt] contents] ;
   :global contentLen [ :len $content ] ;
   
   :global lineEnd 0;
   :global line "";
   :global lastEnd 0;
   
   :do {
         :set lineEnd [:find $content "\n" $lastEnd ] ;
         :set line [:pick $content $lastEnd $lineEnd] ;
         :set lastEnd ( $lineEnd + 1 ) ;
         #If the line doesn't start with a hash then process and add to the list
         :if ( [:pick $line 0 1] != "#" ) do={
   
        :local entry [:pick $line 0 $lineEnd ]
        :if ( [:len $entry ] > 0 ) do={
           /ip firewall address-list add list=MY-IP-LIST address=$entry
        }
      }
   } while ($lineEnd < $contentLen)
   }

How to use

This will grab IP entries for a simple list in the format

   #This is a comment
   #Blah blah blah
   1.1.1.1
   2.2.2.0/24
   3.3.3.3
   4.4.4.128/26

Just substitute the address-list and filename you want to pull from (for anyone else who wants to use it with their own generated lists)

You can use this hand-in-hand with a fetch script to retrive the list from a remote site then process it.

Code for fetching

   /tool fetch address=server.somewhere.tld host=server.somewhere.tld mode=http src-path=folder/anotherfolder/ipaddress.txt
   :delay 10
   #Replace with whatever name you have called the processing script
   /system script run add-ip-addresses