Community discussions

 
vytuz
newbie
Topic Author
Posts: 26
Joined: Mon Jul 31, 2017 3:12 pm

bypass script errors/wrong commands

Wed Jul 10, 2019 11:49 am

Scripting in routeros is not optimized in some important cases. When error occurs, script just stops without logging when and why and further commands were not executed. Most noticed that. If script has error when flashing with netinstall, happens that entire script fails to load. These errors mostly appears because of routeros version changes. I.e. wireless restrictions, bridges. Even upgrading older routers, they can't execute default script after reset. I have no idea how to upgrade routers and make them work after reset, if script tries to execute commands, that newer routeros do not understand. I guess there are no ways to solve that yet. I think there should be option to ignore only specific error and execute current line without bad command (i.e. interface wireless dfs-mode= was removed in routeros, but was used in script).
Another example I can't solve: I want to make one script for several models. One has poe-out, other don't. And I need to disable poe if it exists. So command:
/interface ethernet set [ find default-name=ether5 ] poe-out=off
can be executed if ether5 has poe-out, but if it don't, poe-out can't exist in command line. Are there any ways to make command work on both models?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1239
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: bypass script errors/wrong commands

Wed Jul 10, 2019 1:36 pm

Hmm

A good observation.

You could think this would work:
:do {
  /interface ethernet set [ find default-name=ether5 ] poe-out=off
} on-error={ :put "No poe"}
But since my ether5 does not have poe, you get some like this:
expected end of command (line 1 column 62)
This does not work either:
:put [/interface ethernet  find where (default-name=ether5) && poe-out=auto-on]
It show poe-out=auto-on in red and ignores it and just gives me id of interface 5
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1239
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: bypass script errors/wrong commands

Wed Jul 10, 2019 2:04 pm

You can even test if an interface has poe like this and only run command when interface has poe
:local test [/interface ethernet get ether5]
:if ($test~"poe-out") do={
	:put "yes has poe"
	/interface ethernet set [ find default-name=ether5 ] poe-out=off
} else={
	:put "does not have poe"
}
But it does not help since script gives fail on poe-out=off if you do not have poe on your board.
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
vytuz
newbie
Topic Author
Posts: 26
Joined: Mon Jul 31, 2017 3:12 pm

Re: bypass script errors/wrong commands

Wed Jul 10, 2019 2:26 pm

To test or write poe-out in condition part is not the problem. Problem is to make routeros think, that executable line is not wrong if it is not executed. Maybe it is possible to make "poe-out=off" as string and put it in line then needed?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5913
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: bypass script errors/wrong commands

Wed Jul 10, 2019 2:50 pm

It is a syntax error if parameter does not exist, and you cannot catch these errors at runtime.
One way is to use "parse" command to execute command line based on parameters, which check if poe should exist on this router.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1239
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: bypass script errors/wrong commands

Wed Jul 10, 2019 3:13 pm

One way is to use "parse" command
Can you post an example on this?
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
vytuz
newbie
Topic Author
Posts: 26
Joined: Mon Jul 31, 2017 3:12 pm

Re: bypass script errors/wrong commands

Wed Jul 10, 2019 3:35 pm

One way is to use "parse" command to execute command line based on parameters, which check if poe should exist on this router.
As I imagine it is not an option if I want to flash script with netinstall, is it? As I mentioned, is it possible to use i.e. str1="poe-out=off" as variable and use in command line then needed, or otherwise set str1="" or str1=null or something?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5913
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: bypass script errors/wrong commands

Thu Jul 11, 2019 10:18 am

Basic example:
:global setPoe [:parse ":put \"set poe settings here\"!"];
:if ($poeExist = 1) do={
  $setPoe;
}
 
vytuz
newbie
Topic Author
Posts: 26
Joined: Mon Jul 31, 2017 3:12 pm

Re: bypass script errors/wrong commands

Thu Jul 11, 2019 12:20 pm

I made with your examples something, that should work. I will test it with netinstall. Thanks a lot!
:local poe5 [/interface ethernet get ether5]
:local e5 [:parse "/interface ethernet set [find default-name=ether5] poe-out=off"];
:if ($poe5~"poe-out") do={
$e5
log info "Ether5 POE disabled"
} else= {
log info "No POE on Ether5"
}
 
vytuz
newbie
Topic Author
Posts: 26
Joined: Mon Jul 31, 2017 3:12 pm

Re: bypass script errors/wrong commands

Thu Jul 11, 2019 3:13 pm

Any ideas why this part is not working when I flash with netinstall? I put this code in {}, for local variables to work, but still nothing. It works as script in routeros, but not as a part of booting script, flashed with netinstall.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5913
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: bypass script errors/wrong commands

Thu Jul 11, 2019 5:42 pm

Most likely interface doe snot exist yet when you execute script at startup.
Add delay or loop that waits until interfaces appear.
 
vytuz
newbie
Topic Author
Posts: 26
Joined: Mon Jul 31, 2017 3:12 pm

Re: bypass script errors/wrong commands

Fri Jul 12, 2019 9:22 am

My code starts from waiting interfaces to appear and then executes the rest. I replaced line /interface ethernet set [ find default-name=ether5 ] poe-out=off with this code. So it is not the case. There is something else in code that fails when booting.
 
vytuz
newbie
Topic Author
Posts: 26
Joined: Mon Jul 31, 2017 3:12 pm

Re: bypass script errors/wrong commands

Fri Jul 12, 2019 9:53 am

I found problem, simplier than I thought. Just missed : before log commands. Working code in booting is:

{
:local poe5 [/interface ethernet get ether5];
:local e5 [:parse "/interface ethernet set [find default-name=ether5] poe-out=off"];
:if ($poe5~"poe-out") do={
$e5;
:log info "Ether5 POE disabled";
} else= {
:log info "No POE on Ether5";
}
}

I still not exactly understand parse command, but at least it works.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1239
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: bypass script errors/wrong commands

Fri Jul 12, 2019 10:54 am

I am learning from this as well, see there are other ways to do things :)

So to clean your code some.
You do not need ; at the end of each line, only when there are multiple commands on same line, use ; to separate it.
Also no need to use variables, use the code directly.
Use tab in if/loop etc to make it more readable.

This should works fine:
{
:if ([/interface ethernet get ether5]~"poe-out") do={
	[:parse "/interface ethernet set [find default-name=ether5] poe-out=off"]
	:log info "Ether5 POE disabled"
} else= {
	:log info "No POE on Ether5"
}
}
PS, you may also remove outer {}
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
vytuz
newbie
Topic Author
Posts: 26
Joined: Mon Jul 31, 2017 3:12 pm

Re: bypass script errors/wrong commands

Fri Jul 12, 2019 12:24 pm

Thanks! It is working also. Log messages could be also removed, used it to see what is happening. {} were used because of local variables.
:if ([/interface ethernet get ether5]~"poe-out") do={
	[:parse "/interface ethernet set [find default-name=ether5] poe-out=off"]
}
Finally we made it simple to bypass syntax logic :D It is very usefull for me to make one code for several models, improve and edit only one script.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1239
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: bypass script errors/wrong commands

Fri Jul 12, 2019 1:53 pm

This did make me update my MikroTik for Splunk script to handle when routers does not have temperature, so changed from:
:local voltage ([/system health get voltage]/10)
:local temperature ([/system health get temperature])
:log info message="script=health voltage=$voltage V temperature=$temperature C"
to:
:if ([/system health get]~"temperature") do={
	:local voltage ([/system health get voltage]/10)
	:local temperature ([/system health get temperature])
	:log info message="script=health voltage=$voltage V temperature=$temperature C"
}
Even if both did work, its more cleaner to not get error message in the script. (see link my signature for more info)
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 

Who is online

Users browsing this forum: No registered users and 11 guests