Community discussions

MikroTik App
 
greggio
just joined
Topic Author
Posts: 12
Joined: Fri Feb 16, 2024 8:24 pm

Error handling in RouterOS 7 Scripting (ROS7)

Tue Mar 19, 2024 12:21 am

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" }
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3300
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Error handling in RouterOS 7 Scripting (ROS7)

Wed Mar 20, 2024 11:36 am

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
}
 
greggio
just joined
Topic Author
Posts: 12
Joined: Fri Feb 16, 2024 8:24 pm

Re: Error handling in RouterOS 7 Scripting (ROS7)

Wed Mar 20, 2024 4:03 pm

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
}
Last edited by greggio on Wed Mar 20, 2024 4:48 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Error handling in RouterOS 7 Scripting (ROS7)

Wed Mar 20, 2024 4:08 pm

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...
 
greggio
just joined
Topic Author
Posts: 12
Joined: Fri Feb 16, 2024 8:24 pm

Re: Error handling in RouterOS 7 Scripting (ROS7)

Wed Mar 20, 2024 5:24 pm

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?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Error handling in RouterOS 7 Scripting (ROS7)  [SOLVED]

Wed Mar 20, 2024 5:41 pm

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.

untested example code

: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....

untested example code

: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.
 
greggio
just joined
Topic Author
Posts: 12
Joined: Fri Feb 16, 2024 8:24 pm

Re: Error handling in RouterOS 7 Scripting (ROS7)

Wed Mar 20, 2024 8:03 pm

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! :)


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!

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests