Get a list of all address-list

Hi

I have a script that prints the number of entry in one address-list like this:

:local fwcount ([/ip firewall address-list print count-only where list~"FW_Block_users"])
:log info message="script=counter fw-block=$fwcount"

But how do I do to get all address-lists?
I do not find any command to get a list of the address-lists

Result should be some like this:
FW_Block_users 231
FW_Test 43

Eks to get all dynamic nat entry, I do use:
/ip firewall nat find dynamic=yes

And to get info from it I do some like this:
:foreach logline in=[/ip firewall nat find dynamic=yes] do={ do some thing with it}

For address-list I do not find any solution.

I think it can be done, but going to be hard.

Iterate over all entries and get the list value (get value-name=list) and add to an array, similar to your foreach at the end
remove duplicates from array which would need to be another script

Thanks for your reply.

I did get the output from your idea like this:

 :foreach id in=[/ip firewall address-list find] do={:put [/ip firewall address-list get value-name=list number=$id]}

Get a count of unique item in the list would solve it.

One problem is that one of the list has between 1500 and 2500 address, so its huge.

As long as you don’t have a lot of different lists, should still be doable, by de-duplicating the array of list names. Best in a separate script.
But since there is no way to check if an element is part of an array, except comparing it with each entry, if there are a lot of lists, it’s going to be slow ~O(n²)!

Algorithm could be:

duplicates array: dups
result array: uniqs

for each element in dups, do
  iterate over all elements of uniqs
    if match found -> continue (with next entry of dups)
  add to uniqs
#trick to create empty array
:local addrcnt [:toarray ""] 
:foreach id in=[/ip firewall address-list find] do={
 :local rec [/ip firewall address-list get $id]
 :local listname ($rec->"list")
 :set ($addrcnt->"$listname") ($addrcnt->"$listname"+1)
  #:put ($addrcnt->"$listname")
}
:foreach k,v in=$addrcnt do={ :put "$k=$v" }

Here is few tricks:

  1. Create empy associative array
  2. Reading each value updating count. Reading count for non-exist value returns None which automatically converted to 0 during increment.
    But write
 :set ($addrcnt->"$listname") (toint($addrcnt->"$listname")+1)

will work too

PS: some code optiimization:

#trick to create empty array
:local addrcnt [:toarray ""] 
:foreach id in=[/ip firewall address-list find] do={
 :local listname [/ip firewall address-list get $id value-name=list]
 :set ($addrcnt->"$listname") ($addrcnt->"$listname"+1)
  #:put ($addrcnt->"$listname")
}