Community discussions

MikroTik App
 
andrei
newbie
Topic Author
Posts: 27
Joined: Wed Oct 29, 2014 9:53 am

no such item breaks execution

Mon Jan 11, 2016 7:28 pm

I need to do a simple test to check if a connection is alive.
The connection has a connection mark and I just need to test if it exists or not.
If it exists the script will send an email or log it.

I have this:
if ([/ip firewall connection get value-name=connection-mark [find connection-mark="KRN"]]="KRN") do={ :log warning "KRN is OK;} else={ :log warning "KRN is gone"; }
If the connection exists all is fine. If it doesn't exist the script will be interrupted by "no such item" and it will not anything after that.
I need a way to catch this error or some sort of test so I can tell the script to do something when there is no connection.
Either that or a better way to test the existance of the connection.

Thanks
 
User avatar
ZeroByte
Forum Guru
Forum Guru
Posts: 4051
Joined: Wed May 11, 2011 6:08 pm

Re: no such item breaks execution

Mon Jan 11, 2016 7:34 pm

use on-error={} for the "it's down" condition instead of else={}

in your command, "get" is expecting at least 1 item in its list, and when it gets nothing from the find command, the arguments aren't being supplied correctly so it is actually failing to run. on-error{} gives the script a branch to follow in this case.

Or you could change your logic to something like this:

/ip firewall connection
if ( [ :len [ find where conection-mark="KRN" ] ] > 0 ) do={ :log warning "KRN is OK" } else={ :log warning "KRN is gone" }
When given a spoon,
you should not cling to your fork.
The soup will get cold.
 
andrei
newbie
Topic Author
Posts: 27
Joined: Wed Oct 29, 2014 9:53 am

Re: no such item breaks execution

Mon Jan 11, 2016 7:59 pm

Thanks a lot.
I went for the second choice which seems cleaner and I have used it in other programming languages. I just didn't know there was a length function in the scripting language.
Thanks again, I've been struggling a bit with this although now it seems extremely easy.
 
User avatar
ZeroByte
Forum Guru
Forum Guru
Posts: 4051
Joined: Wed May 11, 2011 6:08 pm

Re: no such item breaks execution

Mon Jan 11, 2016 8:01 pm

I went for the second choice which seems cleaner and I have used it in other programming languages. I just didn't know there was a length function in the scripting language.
In your case, I definitely agree.

Do put on-error{} in your mental toolbox, though, because sometimes that's how it's done. I used that for my dyndns update script, for instance, because if the tool fails, I want the script to keep running and log this information.
When given a spoon,
you should not cling to your fork.
The soup will get cold.

Who is online

Users browsing this forum: No registered users and 21 guests