Community discussions

MikroTik App
 
Sipmud
just joined
Topic Author
Posts: 1
Joined: Thu Sep 17, 2020 11:20 am

Importing IP List from file

Thu Sep 17, 2020 11:32 am

Hello.

I'm struggling with getting to run script that I found to import IP addresses from file and adding them to my address list.
Idea is to fetch file with all official country IP addresses from Web page (file name is "local.net") and add them to list.
Fetching script is working fine, bet when I run the script to add IP to list I get only first number added to my address list.
Screenshot 2020-09-17 at 11.27.53.png
Also, is there "4096 byte read/write limit" workaround?

Maybe there is some scripting guru that can help me?

Script that I am using is:
  :if ( [/file get [/file find name=local.net] size] > 0 ) do={
    /ip firewall address-list remove [/ip firewall address-list find list=Test]
    :local content [/file get [/file find name=local.net] contents] ;
    :local contentLen [ :len $content ] ;
    :put ( "=>" . $content . "<=" )
    :local lineEnd 0
    :local line ""
    :local lastEnd [ :tonum 0 ]


    :do {
      :set line [ :pick $content $lastEnd [ :find $content "\r\n" $lastEnd ] ]
      :set lineEnd [:find $content "\n" $lastEnd ] ;
      :put "lineEnd=$lineEnd< - lastEnd=$lastEnd - line=$line<"
    :if ( [ :len $content ] = 0 ) do={
      :put "UNIX"
      :set line [ :pick $content $lastEnd [ :find $content "\r\n" $lastEnd ] ]
       :set lineEnd [:find $content "\n" $lastEnd ] ;
    }
      :set lastEnd ( $lineEnd + 1 ) ;

      :if ( [:pick $line 0 1] != "\n" ) do={
      :put ">$line<"
        :if ( [:len $line ] > 0 ) do={ /ip firewall address-list add list=Test address=$line }
      }
    } while ($lineEnd < $contentLen)
  }
You do not have the required permissions to view the files attached to this post.
 
Lelik200
just joined
Posts: 22
Joined: Fri Jul 24, 2020 1:48 pm

Re: Importing IP List from file

Fri Oct 09, 2020 9:49 pm

Please post local.net file example.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 1996
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Importing IP List from file

Sat Oct 10, 2020 10:09 am

You should prepare your list before import.
Read this topic: viewtopic.php?p=606832#p606832
Real admins use real keyboards.
To quote or not to quote, there is the topic: viewtopic.php?f=2&t=168474
 
gsbiz
just joined
Posts: 12
Joined: Sat Nov 17, 2018 5:18 pm

Re: Importing IP List from file

Sat Oct 10, 2020 5:37 pm

Try these 5 lines.
just add your own TLD's, it will add an address list for each TLD. Run it again to update them. You will need to create a firewall rule to drop the TLD lists. Beware the lists are huge (who woulda guessed) and if you load them into memory they will take it all, so select your TLD's well and don't go for too many, or at least monitor your memory level.

All the best.
foreach i in={ "NL"; "CN"; "RU"; "DE"; "UA"} do={
/tool fetch url="http://www.iwik.org/ipcountry/mikrotik/$i" dst-path=/DISK/downloads/$i;
/ip firewall address-list remove [/ip firewall address-list find list=$i];
/import file-name=/DISK/downloads/$i;
/file remove /DISK/downloads/$i;
}
 
msatter
Forum Guru
Forum Guru
Posts: 2154
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Importing IP List from file

Sat Oct 10, 2020 5:48 pm

Reading pure IP adresses is possible up to 64KB large files.

viewtopic.php?f=9&t=152632

I am on the moment busy to create backup/restore for adresslists present in the router and it will export a .RSC file that smaller than the normal export.
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet, no Amazon/Cloudfront/AWS.

Running:
RouterOS 6.49Beta / Winbox 3.27 64bits
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 1996
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Importing IP List from file

Sat Oct 10, 2020 6:32 pm

I recall this ... not perfect but it could help
viewtopic.php?f=23&t=148187&p=729368#p729368
Real admins use real keyboards.
To quote or not to quote, there is the topic: viewtopic.php?f=2&t=168474
 
garethiowc
just joined
Posts: 15
Joined: Wed Dec 03, 2014 12:33 am

Re: Importing IP List from file

Tue Apr 27, 2021 12:39 pm

Try these 5 lines.
just add your own TLD's, it will add an address list for each TLD. Run it again to update them. You will need to create a firewall rule to drop the TLD lists. Beware the lists are huge (who woulda guessed) and if you load them into memory they will take it all, so select your TLD's well and don't go for too many, or at least monitor your memory level.

All the best.
foreach i in={ "NL"; "CN"; "RU"; "DE"; "UA"} do={
/tool fetch url="http://www.iwik.org/ipcountry/mikrotik/$i" dst-path=/DISK/downloads/$i;
/ip firewall address-list remove [/ip firewall address-list find list=$i];
/import file-name=/DISK/downloads/$i;
/file remove /DISK/downloads/$i;
}
When I run this script I get the following error - expected end of command (line 2 column 86)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 2203
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Importing IP List from file

Tue Apr 27, 2021 1:18 pm

There is a bug in folder part of the script. Folder not needed so this does work fine.
foreach i in={ "NL"; "CN"; "RU"; "DE"; "UA"} do={
	/tool fetch url="http://www.iwik.org/ipcountry/mikrotik/$i" dst-path=$i
	/ip firewall address-list remove [/ip firewall address-list find list=$i]
	/import file-name=$i
	/file remove $i
}
 
Try Splunk> to monitor your MikroTik Router(s). How to set it up. :mrgreen:

MikroTik->Splunk
 
 
garethiowc
just joined
Posts: 15
Joined: Wed Dec 03, 2014 12:33 am

Re: Importing IP List from file

Tue Apr 27, 2021 1:21 pm

Awesome thanks

Who is online

Users browsing this forum: rextended and 32 guests