Community discussions

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

Exporting IP addresses from an address-list to a file

Fri Jan 26, 2024 6:24 pm

Hi, I have this script that exports the IP addresses of this list to a file.

/ip firewall address-list
:foreach id in=[find list=servers] do={
    :local ipAddr [:toip [get $id address]] 
    :local filenameIP "servers100.txt"
    /file
    :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filenameIP "flash/$filenameIP"}
    :if ([:len [find where name=$filenameIP]] = 0) do={print file="$filenameIP"; :delay 2s; set $filenameIP contents=""}
    :local filecontent [get $filenameIP contents]
    :local newfilecontent "$ipAddr\r\n$filecontent"
    set $filenameIP contents=$newfilecontent; :delay 1s
}

But I have a problem and if I run the script again it adds ALL the IPs again, I would like please to indicate me an "IF" condition to compare the "address-list" with the content of the file and only add the new IPs that are not in the file.

Is this possible? Thank you.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 320
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Exporting IP addresses from an address-list to a file

Fri Jan 26, 2024 9:44 pm

Can I search for content within a file?
/ip firewall address-list
:foreach id in=[find list=servers] do={
    :local ipAddr [:toip [get $id address]] 
    :local filenameIP "servers100.txt"
    /file
    :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filenameIP "flash/$filenameIP"}
    :if ([:len [find where name=$filenameIP]] = 0) do={print file="$filenameIP"; :delay 2s; set $filenameIP contents=""}
    :local filecontent [get $filenameIP contents]
    
    :foreach id2 in=[$filecontent] do={
        :if ((($ipAddr in $id2) or ($ipAddr = $id2)) = 0) do={
        >>>>> NOT WORKING <<<<<<<    
        }  
    }
    <
    :local newfilecontent "$ipAddr\r\n$filecontent"
    set $filenameIP contents=$newfilecontent; :delay 1s
}

Who is online

Users browsing this forum: No registered users and 12 guests