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: 4047
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" }
 
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: 4047
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.
 
User avatar
floaty
Member
Member
Posts: 325
Joined: Sat Oct 20, 2018 1:24 am
Location: 52°08'32.34"N 14°39'05.0"E

Re: no such item breaks execution

Fri Oct 27, 2023 11:53 pm

very helpful !
.
after finding the post, ... I needed 'my time' to skip the 'if'-thing ... but now it's done ... jfc ... :shock:
.
.
:do {/interface/wireless/registration-table/get [/interface/wireless/registration-table/find where interface=wlan1]} on-error={
/interface/bridge/set w_bridge disabled=yes
:delay 1s
/interface/bridge/set w_bridge disabled=no 
};
.
.
the snippet below, looked very good for me ... for hours 8) ... but "no such item" is a ruthless one :?
.
:if ([/interface/wireless/registration-table/get [/interface/wireless/registration-table/find where interface=wlan1] ap] =true) do={ :put on } else={ 
/interface/bridge/set w_bridge disabled=yes
:delay 1s
/interface/bridge/set w_bridge disabled=no 
};
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12032
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: no such item breaks execution

Sat Oct 28, 2023 1:30 pm

If the scripts were written correctly, they would not give errors.

If the purpose of the script is to
if printing only the count of the interface registered on wlan1, return 0, disable the bridge w_bridge and after 1 second enable it again (for what purpose???),
that is certainly not the way.
 
User avatar
floaty
Member
Member
Posts: 325
Joined: Sat Oct 20, 2018 1:24 am
Location: 52°08'32.34"N 14°39'05.0"E

Re: no such item breaks execution

Sat Oct 28, 2023 4:56 pm

purpose of the script is to reset the w_bridge interface when interface wlan1* loses the connection to it's SSID and release the dhcp-client on w_bridge ...
to give finally way for the lower-priority default-route ... on another dhcp-client-interface ...
.
... there may be other opportunities ?! ... suggestions ?
.
first script works fine ...
.
second one ... not so ... because: "no such item" is a ruthless one : ) ... ( in this post just to illustrate, that running in the wrong direction ... mostly is a long run )
.
.
*) bridge-port of w_bridge
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12032
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: no such item breaks execution

Sun Oct 29, 2023 3:21 am

Why not simply release the dhcp instead of disabling bridge for do that?
Something that I do not have considered?
 
User avatar
floaty
Member
Member
Posts: 325
Joined: Sat Oct 20, 2018 1:24 am
Location: 52°08'32.34"N 14°39'05.0"E

Re: no such item breaks execution

Mon Oct 30, 2023 2:06 am

good point ... guess since the problem was the default-route, I engaged on the related interface ... habit
... releasing the dhcp-client is way cleaner ... and without the delay-thing ... and single line
good point ... changed ! ... thanks
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12032
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: no such item breaks execution

Mon Oct 30, 2023 10:19 am

:) 

Who is online

Users browsing this forum: No registered users and 12 guests