Correction for Simple Queue to Address list?

I need to copy target addresses from Simple Queue to an Address list.
In order to do that I use the following script:

:foreach i in=[ /queue simple find ] do={
   :local ip [/queue simple get value-name=target $i ]
   /ip firewall address-list add list=List address=$ip
}

The problem is that I have some queues with multiple targets so if I use :put
instead of adding to an address list I get something like:

192.168.10.2/32;192.168.11.1/27
192.168.169.5/32
192.168.169.10/32

How do I go about separating these multiple targets and adding them as
separate entries into an address list?

Thanks!

AFAIK, the target would be an array so you can just foreach over that.

BTW, it will be a lot more efficient (CPU wise) to use print as-value… So:

:foreach i in=[/queue simple print as-value] do={
    :foreach j in=($i->"target") do={
        /ip firewall address-list add list=List address=$j
    }
}

Thanks a lot, that is what I was looking for!!

But now there is a problem when there are more queues with the same target address
(but different Packet Marks). When “/ip firewall address-list add” encounters duplicate
address it breaks the script with error “failure: already have such entry” and doesn’t
add the rest of the addresses to the list. How could that be avoided?

Besides, one note if someone else try to use it… for the script you wrote to work in
RouterOS 5.x it needs a small modification in the second line. It should be like this:

:foreach j in=($i->"target-addresses") do={

And in version 5.x it doesn’t compalin about duplicate entries in the address list.

You could either check if there’s one with “find count-only” being different from 0, or you could use “do {} on-error”. I don’t know which is more efficient, but purely on the basis of “cleanness”, I think the “find count-only” way is better.

To minimize the menu changeups, the menu could be moved on top, and “:” being used on the queues, so…

/ip firewall address-list
:local listName "List"
:foreach i in=[:queue simple print as-value] do={
    :foreach j in=($i->"target") do={
        :if ([find count-only where list=$listName && address=$j] = 0) {
            add list=$listName address=$j
        }
    }
}



5.x doesn’t support associative arrays, so that won’t work there at all. It only has non-associative arrays, where you needed to know the position of each property value to use it (with :pick), and you hoped MikroTik would not modify it in the next release (which I don’t know if they did, as I’d never used scripts on queues myself back then) OR what most people did is what you did in your first post - use print to find the item’s number, then do a separate “get” for each of its properties. Much less efficient, but far more stable between versions.

Thank you very much!

Back to this old thread - could you please help me - I have a little bit different task. I am reading simple queue list and put them to file (or log) as mothly stats. But if I have one queue for multiple targets, my script put that line multiple times for each targes. So is there a way to read only the one (first) target from each queue to variable? Thanks

so I can reply to myself..here is the solution, even though do not know why it works :slight_smile:

while this command:

put [/queue simple get 5 target]

shows 10.0.1.112/28;10.0.1.251/32;10.0.1.252/32;10.0.1.253/32

then this command:

put [:pick [/queue simple get 5 target]]

shows only the first target 10.0.1.112/28