Error handling in RouterOS 7 Scripting (ROS7)

Hi guys!

I have a standard script that I use to configure Mikrotiks in my work.
I’m trying to adapt the script so that would work in ROS6 and ROS7, but i’m not having lucky when testing this part on latest version of ROS7.
The code is supposed to result in error in one of the versions, that’s why I’m using “on-error”.
The problem is that the error is not being handled as I programmed in code, instead it only shows that that is having an error.
Am I making this wrong? Appreciate the help.


expected end of command (line 6 column 20)

# NTP
do {
    /system
    	clock set time-zone-name=Greenwich
    	ntp client set enabled=yes
   	ntp client set primary-ntp=0.0.0.0
    	ntp client set secondary-ntp=0.0.0.0
} on-error= { :put "Ignoring - RouterOS v7" }

do {
    /system
        clock set time-zone-name=Greenwich
        ntp client set enabled=yes
        ntp client servers add address=0.0.0.0
        ntp client servers add address=0.0.0.0
} on-error={ :put "Ignoring - RouterOSv6" }

Instead of using on-error, find out what version of RouterOS the router is running and the run the correct part with if:
Sample code to see main version:

{
:local ver [:tonum [:pick [/system resource get version] 0 1]] 
:put $ver
}

Thanks @Jotne !
I change my approach on how to solve the problem just like you said and it’s working now!

:local rOSversion [:tonum [:pick [system resource get version]]]

:if (num ($rOSversion)<7) do={
    /system
    clock set time-zone-name=Greenwich
    ntp client set enabled=yes
    ntp client set primary-ntp=0.0.0.0
    ntp client set secondary-ntp=0.0.0.0
} else={
    /system
    clock set time-zone-name=Greenwich
    ntp client set enabled=yes
    ntp client servers add address=0.0.0.0
    ntp client servers add address=0.0.0.0
}

No matter how you write the script,
sys ntp client servers provide error on v6 and the script is not executed at all.

You can not prevent the error if the script itself have bad syntax inside… is never run…

Actually it’s not working, when I test on v6 it shows error on the line of v7 and when I test on v7 it shows error on the line of v6.
I thought that using a condition, the part that doesn’t match would be ignored, but it doesn’t work that way.

It’s possible to make this work somehow? Or RouterOS will always interpret the whole script?

Of course there is a way.
Even more than one.

For example make two scripts,
and call from a third script only the correct script for that version.

:if ([/system resource get version]~"^7") do={
    # call script for v7
    /system script run v7script
} else={
    # call script for v6
    /system script run v6script
}

Or you can ignore the syntax difference altogether....

:execute "/system clock set time-zone-name=Greenwich"
:execute "/system ntp client set enabled=yes"
:execute "/system ntp client set primary-ntp=1.1.1.1 secondary-ntp=2.2.2.2"
:execute "/system ntp client add address=1.1.1.1"
:execute "/system ntp client add address=2.2.2.2"

In the latter way, each version applies its own commands and ignores errors.

Thanks for the answer, rextended!
By the way, your posts here in the forum already helped me a lot since I started working with Mikrotik, thanks! :slight_smile:


I tested both ways you mentioned in ROSv7 and ROSv6 and it work flawless.
Decided to go with the second method for simplicity.

So the result was:

    # ROSv6
    :execute "/system clock set time-zone-name=Greenwich"
    :execute "/system ntp client set enabled=yes"
    :execute "/system ntp client set primary-ntp=0.0.0.0"
    :execute "/system ntp client set secondary-ntp=0.0.0.0"

    # ROSv7
    :execute "/system clock set time-zone-name=Greenwich"
    :execute "/system ntp client set enabled=yes"
    :execute "/system ntp client servers add address=0.0.0.0"
    :execute "/system ntp client servers add address=0.0.0.0"

Thanks rextended and Jotne!