Page 1 of 1

How to create a script that does a for on interfaces and creates global variables for each interface with their properti

Posted: Sat Apr 08, 2023 7:07 pm
by williankevenis
How to create a script that does a for on interfaces and creates global variables for each interface with their properties.
The way below doesn't work.
Version RouterOS: 6.48.6 and 7.8
:foreach iface in=[/interface ethernet find] do={
  :local name [/interface ethernet get $iface name]
  :local mac [/interface ethernet get $iface mac-address]
  :local comment [/interface ethernet get $iface comment]
  :local running [/interface ethernet get $iface running]
  :local disabled [/interface ethernet get $iface disabled]

  :global ("eth" . $iface . "_name") $name
  :global ("eth" . $iface . "_mac") $mac
  :global ("eth" . $iface . "_comment") $comment
  :global ("eth" . $iface . "_running") $running
  :global ("eth" . $iface . "_disabled") $disabled
}

Re: How to create a script that does a for on interfaces and creates global variables for each interface with their prop

Posted: Sat Apr 08, 2023 10:07 pm
by tomislav91
The script uses a for-each loop to iterate over all Ethernet interfaces found by the find command.

Inside the loop, the script retrieves the name of the current interface using the get command and stores it in a local variable named name.

The script then sets two global variables using the global command, constructed using the interface name and property name separated by a dot: $name.mac and $name.speed.

The value of the mac-address and speed properties for the current interface are retrieved using the get command.


Overall, the script effectively sets global variables for the MAC address and speed of each Ethernet interface, with the variable names constructed dynamically based on the interface name.
/interface ethernet
:foreach iface in=[find] do={
  :local name [get $iface name]
  :global "$name.mac" [get $iface mac-address]
  :global "$name.speed" [get $iface speed]
}

Re: How to create a script that does a for on interfaces and creates global variables for each interface with their prop

Posted: Sun Apr 09, 2023 12:16 am
by rextended
@tomislav91
Why don't you do a little test to verify the crap you write before posting it on the forum?

/interface ethernet
:foreach iface in=[find] do={
  :local name [get $iface name]
  :global "$name.mac" [get $iface mac-address]
  :global "$name.speed" [get $iface speed]
}

When do you stop inventing commands or asking ChatGPT?

(Is not possible define directly a variable name using another variable content)

Re: How to create a script that does a for on interfaces and creates global variables for each interface with their prop

Posted: Sun Apr 09, 2023 12:29 am
by rextended
@williankevenis
[…]
  :global ("eth" . $iface . "_name") $name
  :global ("eth" . $iface . "_mac") $mac
  :global ("eth" . $iface . "_comment") $comment
  :global ("eth" . $iface . "_running") $running
  :global ("eth" . $iface . "_disabled") $disabled
[…]
If assuming your code works, you would get something like:
eth*8_name = ether1
eth*8_mac = 00:11:22:.....
.....
eth*12_name = ether5
eth*12_mac = 00:11:22:.....
.....
eth*1_name = sfp1
eth*1_mac = 00:11:22:.....
eth*2_name = sfp2
eth*2_mac = 00:11:22:.....


Is what you want???

And, for hypothesys, you can achieve that, how you know on successive point what were the variables called?

It makes no sense, if you want to know the MAC of the ether1, go read it directly, instead of creating these variables in a cumbersome way,
which you then don't know later in the scripts what they are called...

Better that you explain exactly what you want to achieve in the end, so that I and the others can help you.


On meantime waiting some more precise info for you, take this....
:global ifarray [:toarray ""]

/interface ethernet
:foreach iface in=[find] do={
  :set ($ifarray->"ether$iface_name"    ) [get $iface name       ]
  :set ($ifarray->"ether$iface_mac"     ) [get $iface mac-address]
  :set ($ifarray->"ether$iface_comment" ) [get $iface comment    ]
  :set ($ifarray->"ether$iface_running" ) [get $iface running    ]
  :set ($ifarray->"ether$iface_disabled") [get $iface disabled   ]
}

:foreach name,value in=$ifarray do={:put "$name = $value"}