Ethernet Interface print detail

I’m not entirely certain why I developed this, I thought it would be easyish (it was not) and a fun challenge (painful more like).

Anyhow, this script prints bits of interface ethernet and formats it in to a fairly neat table.
Is it good code? probably not, but it seems to do what I say it does. It only seems to have a hard time formatting where the original interface name is much longer or shorter then the rest.

[
/interface ethernet
:put "Name\tModified name\t\t\t\tOrig-mac\t\tCurrent-mac\t\tMTU\tL2MTU"
:foreach IF in=[find] do={
:local var2 [get $IF default-name]
:local var3 [get $IF name]
:local var4 [get $IF orig-mac-address]
:local var5 [get $IF mac-address]
:local var6 [get $IF mtu]
:local var7 [get $IF l2mtu]

:local var20
:if ([:len $var3] > 0) do={:set $var20 " \t\t\t "}
:if ([:len $var3] > 8) do={:set $var20 " \t\t "}
:if ([:len $var3] > 16) do={:set $var20 " \t "}
:if ([:len $var3] > 22) do={:set $var20 " "}
:put "$var2\t$var3\t$var20\t$var4\t$var5\t$var6\t$var7"
}
]
/

Name    Modified name                           Orig-mac                Current-mac             MTU     L2MTU
ether1  ether1 -- External                      74:4D:28:64:D7:90       74:4D:28:64:D7:90       1500    1592
ether2  ether2 -- 192.168.1.7                   74:4D:28:64:D7:91       74:4D:28:64:D7:91       1500    1592
ether3  ether3 -- 192.168.1.8                   74:4D:28:64:D7:92       74:4D:28:64:D7:92       1500    1592
ether4  ether4 -- 192.168.1.9                   74:4D:28:64:D7:93       74:4D:28:64:D7:93       1500    1592
ether5  ether5 -- 192.168.1.12                  74:4D:28:64:D7:94       74:4D:28:64:D7:94       1500    1592
ether6  ether6                                  74:4D:28:64:D7:95       74:4D:28:64:D7:95       1500    1592
ether7  ether7 - Somewhere---- Switch           74:4D:28:64:D7:96       74:4D:28:64:D7:96       1500    1592
ether8  ether8                                  74:4D:28:64:D7:97       74:4D:28:64:D7:97       1500    1592
ether9  ether9                                  74:4D:28:64:D7:98       74:4D:28:64:D7:98       1500    1592
ether10 ether10 - 192.168.1.3                   74:4D:28:64:D7:99       74:4D:28:64:D7:99       1500    1592
sfp-sfpplus1    sfp-sfpplus1                            74:4D:28:64:D7:9A       74:4D:28:64:D7:9A       1500    1600

Feel free to modify, plagiarise or provide feedback if you think it could be improved.

1 Like

When you copy something, at least copy it well...

1 Like

The (good ol') trick we used in DOS batches was to APPEND to the variable value a number of characters (spaces or underscores) and then TRIM it to a given length (in practice transforming the table records from delimited to fixed length).

BUT in Ros we have this nice feature discovered by Amm0:

in practice you place the beginning of each string (variable value) at a given column by "going back" to column 0 and moving forward for the desired number of characters/tabs.

3 Likes

If there are not too many entries (not thousands or more), you can use print as-value to fetch all data as array, to reduce the number of [get] commands to be executed, combined with the \r trick from @Amm0:

:put "Name\t\t\t\t\tModified name\t\t\t\tOrig-mac\t\tCurrent-mac\t\tMTU\tL2MTU"
:foreach d in=[/interface ethernet print as-value proplist=default-name,name,orig-mac-address,mac-address,mtu,l2mtu] do={
    :put "$($d->"default-name")\
        \r\t\t\t\t\t$($d->"name")\
        \r\t\t\t\t\t\t\t\t\t\t$($d->"orig-mac-address")\
        \t$($d->"mac-address")\
        \t$($d->"mtu")\
        \t$($d->"l2mtu")"
}

If you are lazy and don't care about efficiency then proplist=default-name,name,orig-mac-address,mac-address,mtu,l2mtu can also be replaced with detail. But it will consume more CPU and RAM.

:put "Name\t\t\t\t\tModified name\t\t\t\tOrig-mac\t\tCurrent-mac\t\tMTU\tL2MTU"
:foreach d in=[/interface ethernet print as-value detail] do={
    :put "$($d->"default-name")\
        \r\t\t\t\t\t$($d->"name")\
        \r\t\t\t\t\t\t\t\t\t\t$($d->"orig-mac-address")\
        \t$($d->"mac-address")\
        \t$($d->"mtu")\
        \t$($d->"l2mtu")"
}
1 Like

At the most those could be 61 lines on the:

CRS354-48G-4S+2Q+RM

49 ethernet+4 sfpplus+2 (please read as 8) qsfpplus=61

1 Like

The \r trick, when combined with going one character before and putting a <space>, avoids creating stuck strings, but at most only truncated ones... :wink: