Community discussions

MikroTik App
 
User avatar
mutluit
Member
Member
Topic Author
Posts: 421
Joined: Wed Mar 25, 2020 4:04 am

How to set the same field of all list members to the same value?

Sat May 23, 2020 3:43 pm

With this command I can show the contents of the "address" fields of the list items:

/ip service> :foreach i in=[find] do={ :local j [get $i]; :put ($j->"address") }
192.168.20.1/32
192.168.20.1/32
...

Now, how can I do the opposite: setting the "address" fields of all of the list members to the same value, say "192.168.20.2/32" ?
I tried this, but it gives error:

/ip service> :foreach i in=[find] do={ :local j [get $i]; set ($j->"address"="192.168.20.2/32") }
no such item

Update:
Even the following is not working:
/ip service> :foreach i in=[find] do={ :local j [get $i]; :set $j { address="192.168.20.2/32" }}

That variant was used in the following thread viewtopic.php?f=1&t=161299 ,
but in this very scenario here it's somehow not working!
Man, what is this for a frickin syntax with this scripting language? :-) I'm trying to find a logic in this :-)
Last edited by mutluit on Sat May 23, 2020 5:36 pm, edited 1 time in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1557
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: How to set the same field of all list members to the same value?  [SOLVED]

Sat May 23, 2020 5:35 pm

This should do:
:foreach i in=[find] do={set $i address=192.168.20.2/32}
or since its just one IP and no subnet:
:foreach i in=[find] do={set $i address=192.168.20.2}
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
mutluit
Member
Member
Topic Author
Posts: 421
Joined: Wed Mar 25, 2020 4:04 am

Re: How to set the same field of all list members to the same value?

Sat May 23, 2020 5:39 pm

This should do:
:foreach i in=[find] do={set $i address=192.168.20.1/32}
or since its just one IP and no subnet:
:foreach i in=[find] do={set $i address=192.168.20.1}

Man, cool! This indeed works! You are my savior! :-) Thx!

@Jotne, just out of curiosity: how would your inverse function of that look like, ie. outputting the address field of all in the list?
The following does not work (it just outputs some empty lines) :
/ip service> :foreach i in=[find] do={ :put { $i address } }
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1557
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: How to set the same field of all list members to the same value?

Sat May 23, 2020 5:55 pm

You are welcome.

As for the first part, to get information, you could do:
:foreach i in=[find] do={:put ([get $i]->"address")}
You need to handle it as an array. To get a kv field you do use the $array->"field" Where the array her are found by the [get $i]
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 

Who is online

Users browsing this forum: No registered users and 25 guests