Community discussions

MikroTik App
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Export address list to a file

Thu Jan 20, 2022 6:47 pm

Hello, I am using this loop to obtain the IP addresses of a list of addresses such as Blacklist. Running it in Terminal I get the addresses correctly.
{
:foreach addr in=[/ip firewall address-list find list=Blacklist] do={
  :local ipAddr [/ip firewall address-list get $addr address];
  :put $ipAddr
}
}
Result:

216.218.206.0/24
146.88.240.0/24
37.49.229.222
143,198,215,243
184.105.247.0/24
184.105.139.0/24
184.105.143.0/24
64.62.197.0/24
170.106.115.0/24
65.49.20.0/24

I'm trying to get that dump of IPs to put it in a file, I've tried some options without success. For example:
{
:foreach addr in=[/ip firewall address-list find list=Blacklist] do={
  :local ipAddr [/ip firewall address-list get $addr address];
  /file print file=MyList
  delay 1s
  /file set MyList.txt contents=$ipAddr
}
}
Any help please?
BR.
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Export address list to a file

Thu Jan 20, 2022 7:01 pm

Instead of /file print add the IPaddress to content. This is possible till 63KB in size.

Then you write the content to file in one go.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Export address list to a file

Thu Jan 20, 2022 7:04 pm

Instead of /file print add the IPaddress to content. This is possible till 63KB in size.

Then you write the content to file in one go.
Can you give me an example please?
BR.
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Export address list to a file

Thu Jan 20, 2022 7:57 pm

Sometng like this and this allows you to store up to 63KB of data:
{
:foreach addr in=[/ip firewall address-list find list=Blacklist] do={
  :set $data ($data."\r\n".[/ip firewall address-list get $addr address])
 }; #foreach
 /file/set MyList.txt contents=$data
}
"\r\n" is a line return and linefeed stored between each IP addresses.

If you want to store larger lists then this is the way to go:
execute :execute <expression>
Execute the script in background. Result can be written in file by setting file parameter.
{
:local j [:execute {/interface print follow where [:log info ~Sname~]}];
:delay 10s;
:do { /system script job remove $j } on-error={}
}
https://wiki.mikrotik.com/wiki/Manual:Scripting

Who is online

Users browsing this forum: Bing [Bot] and 27 guests