Community discussions

MikroTik App
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Compare RouterOS version in script

Fri May 12, 2023 7:07 pm

I need help to test for routerOS version. Since 7.10xxx user the new time format, how do I test for 7.10 or newer.

7.8 no
6.49 no
7.1 no
7.10beta5 yes
7.10rc1 yes
7.24 yes
8.1beta22 ok


Tried to extract the routerOS version, but not sure how to make a regex true that will work on all version.
It has been simpler if 7.1 was named 7.01

tonum does not work with decimal.

This just removes the first dot
[
:local ver [/system resource get version]
:local dot [:find $ver "."]
:local new "$[:pick $ver 0 $dot]$[:pic $ver ($dot + 1) 99]"
:put $new
]


This gives 710beta5 (development)
I can remove beta5... using a for each loop, but still how to test against 710 and not get error with 71 (fail) vs 711 (ok)
There mest be a simpler way to do this?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Compare RouterOS version in script

Sat May 13, 2023 2:23 am

On [/system resource get version] is not indicated the build-time

On [/ip neighbor] version is all together like "7.7 (stable) Jan/12/2023 07:35:45"

This function convert version from system or from neigbot to one array and keep same info (major, minor, patch, channel, build-time) on same position.

version2arr code

:global version2arr do={
    :local ver [:tostr $1]
    :local pos 0
    :local major "" ; :local minor ""; :local patch "" ; :local chnnl "" ; :local build "" ; :local btime ""
    :set pos   [:find $ver "." -1]
    :set major [:pick $ver 0 $pos]
    :set ver   [:pick $ver ($pos + 1) [:len $ver]]
    :if ([:typeof [:find $ver "." -1]] != "nil") do={
        :set pos   [:find $ver "." -1]
        :set minor [:pick $ver 0 $pos]
        :set ver   [:pick $ver ($pos + 1) [:len $ver]]
    }
    :if ([:typeof [:find $ver " " -1]] != "nil") do={
        :set pos [:find $ver " " -1]
        :if ($minor = "") do={
            :set minor [:pick $ver 0 $pos]
        } else={
            :set patch [:pick $ver 0 $pos]
        }
        :set ver [:pick $ver ($pos + 1) [:len $ver]]
    }
    :if ([:typeof [:find $ver "(" -1]] != "nil") do={
        :set pos [:find $ver ")" -1]
        :set chnnl [:pick $ver 1 $pos]
        :set ver [:pick $ver ($pos + 1) [:len $ver]]
    }
    :if ($ver ~ "^ ") do={
        :set btime [:pick $ver 1 [:len $ver]]
    } else={
        :set btime $ver
    }
    :return ($major,$minor,$patch,$chnnl,$btime)
}

examples code

[] > :put [$version2arr [/system resource get version]]
6;48;6;long-term

[] > :put [$version2arr [/system resource get version]]
7;7;;stable

[] > :put [$version2arr "7.7 (stable) Jan/12/2023 07:35:45"]
7;7;;stable;Jan/12/2023 07:35:45

[] > :put [$version2arr "7.10beta5 (development) 2023-05-03 14:33:19"]
7;10beta5;;development;2023-05-03 14:33:19

About development versions, the minor version is 10beta5, but some subsequents division is possible, but for me is excessive.
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Compare RouterOS version in script

Sat May 13, 2023 8:13 pm

Thanks. That was a short and simple solution to get the version ;)

MT should make this as a simple and output should be numeric 7.01 7.11 etc instead of 7.1 and 7.11
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Compare RouterOS version in script

Mon May 15, 2023 3:56 pm

I have a function that converts the version to a numerical value:
https://git.eworm.de/cgit/routeros-scri ... 260b#n1269
[admin@MikroTik] > :put [ $VersionToNum 7.9 ]
118095616
[admin@MikroTik] > :put [ $VersionToNum 7.10beta5 ] 
118120197
[admin@MikroTik] > :put ([ $VersionToNum 7.9 ] > [ $VersionToNum 7.10beta5 ])
false
This works with all versions, including "alpha", "beta" and "rc".
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Compare RouterOS version in script

Mon May 15, 2023 5:17 pm

Thanks, will try it out.
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Compare RouterOS version in script

Mon May 15, 2023 7:56 pm

Your scripts are some overwhelming :)
Can you point me where to find the CharacterReplace part? Without need to install everything.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Compare RouterOS version in script

Mon May 15, 2023 8:10 pm

(the date of 10 beta 5 build time is approx, I do not rememmber the correct value)

If you want convert the version to a number, just for compare two numbers, you can do two things:

1)
Convert build date to Unix Epoch, like
7.7 (stable) Jan/12/2023 07:35:45 ==>> 1673508945 (GMT)
7.10beta5 (development) 2023-05-09 13:38:22 ==>> 1683639502 (GMT)

datetime2epoch is here >> viewtopic.php?p=994849#p994849

example code

{
:local a [$datetime2epoch [/system resource get build-time]] ; # read current RouterOBARD value
:local b [$datetime2epoch "Dec/03/2021 12:15:05"] ; # from RouterOS 6.48.6
:local c [$datetime2epoch "2023-05-09 13:38:22"] ; # from RouterOS 7.10beta5
:put ($a > $b)
:put ($b < $c)
:put ($a < $c)
}

And spitting again the patch value only if is present "alpha", "beta", or "rc":
2h)
convert the vesion based on numbers, like what @eworm do:
7.7 stable (is implicit 7.7.0 stable 0) ==>> 7 * 0x10000000 + 7 * 0x100000 + 0 * 0x1000 + "stable = 0x300" + 0 = 0x070700300 (on decimal is 1886388992)
7.10beta5 development (is implicit 7.10.0 beta 5) ==>> 7 * 0x10000000 + 10 * 0x100000 + 0 * 0x1000 + "beta = 0x200" + 5 = 0x071000205 (on decimal is 1895825925)

2d)
or convert it to decimals (is more readable than hex...)
7.7 stable (is implicit 7.7.0 stable 0) ==>> 7 * 10000000 + 7 * 100000 + 0 * 1000 + "stable = 300" + 0 = 70700300 (on hex is 0x436CD0C)
7.10beta5 development (is implicit 7.10.0 beta 5) ==>> 7 * 10000000 + 10 * 100000 + 0 * 1000 + "beta = 200" + 5 = 71000205 (on hex is 0x43B608D)
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Compare RouterOS version in script

Mon May 15, 2023 9:48 pm

Your scripts are some overwhelming :)
Can you point me where to find the CharacterReplace part? Without need to install everything.
https://git.eworm.de/cgit/routeros-scri ... a260b#n155
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Compare RouterOS version in script

Wed Oct 04, 2023 10:01 pm

I have a function that converts the version to a numerical value:
https://git.eworm.de/cgit/routeros-scri ... 260b#n1269
[admin@MikroTik] > :put [ $VersionToNum 7.9 ]
118095616
[admin@MikroTik] > :put [ $VersionToNum 7.10beta5 ] 
118120197
[admin@MikroTik] > :put ([ $VersionToNum 7.9 ] > [ $VersionToNum 7.10beta5 ])
false
This works with all versions, including "alpha", "beta" and "rc".

I was just stopping by to say thanks for the $CharacterReplace and $VersionToNum functions, they are very useful.

Thanks for sharing!
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Compare RouterOS version in script

Thu Oct 05, 2023 1:37 am

Glad I could help, you are welcome.

If you find this useful have a look at all my RouterOS Scripts...

Who is online

Users browsing this forum: bertus and 22 guests