[Backward compatibility] Get First MAC-Address

I made a script that attempt to extract the MAC address from the first interface on the router:

:put [/interface ethernet get 0 mac-address]

It was all working fine until today when I updated one of my routers’ OS version from “5.18” to “6.19”.
The returned MAC address changed from being the first interface to the second.

I am guessing the interfaces changed IDs because their names when sorted alphabetically isn’t in the same order as the default names.
Before update:

Flags: X - disabled, R - running, S - slave 
 #    NAME                MTU MAC-ADDRESS       ARP        MASTER-PORT            SWITCH                                     
 0 R  Wan                1500 xx:xx:xx:xx:xx:FA enabled   
 1 R  Lan1               1500 xx:xx:xx:xx:xx:FB enabled    none                   switch1                                    
 2  S Lan2               1500 xx:xx:xx:xx:xx:FC enabled    Lan1                   switch1                                    
 3  S Lan3               1500 xx:xx:xx:xx:xx:FD enabled    Lan1                   switch1                                    
 4  S Lan4               1500 xx:xx:xx:xx:xx:FE enabled    Lan1                   switch1

After update:

Flags: X - disabled, R - running, S - slave 
 #    NAME                MTU MAC-ADDRESS       ARP        MASTER-PORT            SWITCH           
 0 R  Lan1               1500 xx:xx:xx:xx:xx:FB enabled    none                   switch1          
 1  S Lan2               1500 xx:xx:xx:xx:xx:FC enabled    Lan1                   switch1          
 2  S Lan3               1500 xx:xx:xx:xx:xx:FD enabled    Lan1                   switch1          
 3  S Lan4               1500 xx:xx:xx:xx:xx:FE enabled    Lan1                   switch1          
 4 R  Wan                1500 xx:xx:xx:xx:xx:FA enabled

I found this to work better for version “6.19”:

:put [/interface get [/interface find default-name=ether1] mac-address]

Problem is that the “default-name” property isn’t available in OS version “5.18”.

So is there a way to always get the first interface, no matter the OS version?
Or maybe a way to check which way would work?

I want to be able to use the same script on all my routers no matter the OS version they are running.
I am mostly trying to avoid using the interface’s name if possible, because it could change depending on the router.

Also if someone maybe know at what OS version this was changed, I’d very much like to know too.

Ok waiting for moderator to approve my thread, I found the changelog.

What’s new in 6.5 (2013-Oct-16 15:32):

*) console - exported physical interface configuration uses ‘default-name’
instead of item number to match relevant interface;

That is the only entry that seem to be related to the “default-name” property.
So I am guessing that is when my problem starts.

You could do:
:local rosVersion [/system resource get “version”];
:local rosVersionSep [:find $rosVersion “.”];
:local rosMajorVersion [:tonum [:pick $rosVersion 0 $rosVersionSep]];
:local rosMinorVersion [:tonum [:pick $rosVersion (1+$rosVersionSep)]];

:if ($rosMajorVersion <= 5) do={
:put [/interface ethernet get 0 mac-address];
} else={
:put [/interface get [/interface find default-name=“ether1”] mac-address];
}To make this work in both versions.

Ah so it is possible to use the property as long as it is never reached in the script?
I was under the impression that having an invalid syntax anywhere in the script would interrupt it.

Running some tests and will post my solution when I am done.

Thanks!

Found a few solutions.

The simplest is this one:

:if ([/system resource get "version"] ~ "^5.") do={
    :put [/interface ethernet get 0 mac-address]
} else={
    :put [/interface get [/interface find default-name=ether1] mac-address]
}

Simply check if the version string starts with “5.” or not.
Found one of my routers that was running version 6.1, and “default-name” worked on that one too.
So I would guess it just wasn’t mentioned in the changelog? (annoying but whatever)

My more complicated version (thanks for the basic idea boen_robot) is therefore a little useless, but I’ll share it for fun:

# Get first MAC Address.
{
    :local rosVersion [/system resource get "version"];
    :local rosVersionSep [:find $rosVersion "."];
    :local rosVNum [:tonum [:pick $rosVersion (1+$rosVersionSep) 10]]
# Make sure there are no letters in the version number.
    :if ([:len rosVNum] != 0) do={
        :set rosVNum ([:tonum [:pick $rosVersion 0 $rosVersionSep]] * 100 + $rosVNum)
    } else={
        :set rosVNum ([:tonum [:pick $rosVersion 0 $rosVersionSep]] * 100)
    }
    :put ("Numeric ROS: " . $rosVNum)
# Check if the version before 6.5 and use the correct command to get the MAC Address.
    :if ($rosVNum < 605) do={
        :put [/interface ethernet get 0 mac-address];
    } else={
        :put [/interface get [/interface find default-name="ether1"] mac-address];
    }
}

Converting the version number to an INT and then doing a simple “less than” makes me able to check for a specific version.
But this was only useful before I got to test it on the 6.1 OS version.
I also did add in a check to see if it was even possible to convert the minor version number to an INT in cause it had “beta” or “rc” in it.

Concluesion is that the interface ID behavior changed somewhere between 5.26 and 6.1.