Page 1 of 1

no such item breaks execution

Posted: Mon Jan 11, 2016 7:28 pm
by andrei
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

Re: no such item breaks execution

Posted: Mon Jan 11, 2016 7:34 pm
by ZeroByte
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" }

Re: no such item breaks execution

Posted: Mon Jan 11, 2016 7:59 pm
by andrei
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.

Re: no such item breaks execution

Posted: Mon Jan 11, 2016 8:01 pm
by ZeroByte
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.