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
}
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]
}
@tomislav91
Why don’t you do a little test to verify the crap you write before posting it on the forum?
When do you stop inventing commands or asking ChatGPT?
(Is not possible define directly a variable name using another variable content)
@williankevenis
If assuming your code works, you would get something like:
eth8_name = ether1
eth8_mac = 00:11:22:…
…
eth12_name = ether5
eth12_mac = 00:11:22:…
…
eth1_name = sfp1
eth1_mac = 00:11:22:…
eth2_name = sfp2
eth2_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"}