Community discussions

MikroTik App
 
aleab
Member Candidate
Member Candidate
Topic Author
Posts: 110
Joined: Sat Sep 22, 2018 6:13 pm

simple double if / else error

Thu Mar 14, 2024 10:43 am

Hello,
i'm sorry but i can understand why my script generate an error...
i would check my WAN status and write a simple script that works

i created 3 netwatch and put this on scheduler every 15 sec
:local var1 [:put ([/tool netwatch print as-value where host=8.8.8.8]->0->"status");];
:local var2 [:put ([/tool netwatch print as-value where host=1.1.1.1]->0->"status");];
:local var3 [:put ([/tool netwatch print as-value where host=208.67.220.220]->0->"status");];
:if (($var1 = "down") && ($var2 = "down") && ($var3 = "down")) do={ 
:log error "WAN1 DOWN"
/ip route disable [/ip route find comment="WAN1"];
/ip firewall connection remove [find] ;
} else={
/ip route enable [/ip route find comment="WAN1"];
}
works fine but i have every 15 sec a line in log for enabling route (that's already enabled)

so i try to elaborate this script, with
:local var1 [:put ([/tool netwatch print as-value where host=8.8.8.8]->0->"status");];
:local var2 [:put ([/tool netwatch print as-value where host=1.1.1.1]->0->"status");];
:local var3 [:put ([/tool netwatch print as-value where host=208.67.220.220]->0->"status");];
:if (($var1 = "down") && ($var2 = "down") && ($var3 = "down")) do={ 
:log error "WAN1 DOWN"
/ip route disable [/ip route find comment="WAN1"];
/ip firewall connection remove [find] ;
} else={
#:log info "WAN1 UP"
:local routewan1 [:put [/ip/route/get value-name=active [find comment="WAN1"]]];
:if ($routewan1 = "true") do ={
# nothing
} else={
/ip route enable [/ip route find comment="WAN1"];
/ip firewall connection remove [find] ;
}
}
in scheduler (with approriate firewall rules to simulate WAN1 offline) not work
so i create a script and run in terminal
/system/script/run aaa
expected closing brace (line 18 column 1)

ROS 7.13.5 on RB4011...

i try for hours before posting but i can't find my error...

thank you in advance
 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: simple double if / else error

Thu Mar 14, 2024 5:48 pm

Not an answer to your question, but wouldn't something *like*:
/ip/route/set [/ip/route/find where comment=WAN1 disabled=yes] disabled=no
simplify the script?

As a side note:
/ip firewall connection remove [find] ;
is not entirely correct, it may work or it may not:
viewtopic.php?p=853803#p853800
 
optio
Long time Member
Long time Member
Posts: 675
Joined: Mon Dec 26, 2022 2:57 pm

Re: simple double if / else error

Thu Mar 14, 2024 10:22 pm

replace
:if ($routewan1 = "true") do ={
with
:if ($routewan1 = "true") do={

do (or from, to, else, on-error, ...) with whitespace before = is not correct syntax, but after = whitespace can exists before block brackets.
 
aleab
Member Candidate
Member Candidate
Topic Author
Posts: 110
Joined: Sat Sep 22, 2018 6:13 pm

Re: simple double if / else error

Fri Mar 15, 2024 8:03 am

big thank you both !!!!

i search if else and don't see a space .... :(
i'm an idiot.... :(

to answer @jaclaz because i'm not a programmer , so i have more difficult to "write" a script....

so after a considerationa think this is a definitive script
:local var1 [:put ([/tool netwatch print as-value where host=8.8.8.8]->0->"status");];
:local var2 [:put ([/tool netwatch print as-value where host=1.1.1.1]->0->"status");];
:local var3 [:put ([/tool netwatch print as-value where host=208.67.220.220]->0->"status");];
:if (($var1 = "down") && ($var2 = "down") && ($var3 = "down")) do={ 
:log error "WAN1 DOWN"
/ip route disable [/ip route find comment="WAN1"];
/ip fire conn
:foreach idc in=[find where timeout>60] do={
 remove [find where .id=$idc]
}
} else={
/ip route set [/ip route find where comment=WAN1 disabled=yes] disabled=no
}

is correct?

thank you
 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: simple double if / else error

Fri Mar 15, 2024 11:10 am

Though I have a few years experience in scripting, I am also a beginner in Mikrotik scripting, so don't trust my opinion much, but it seems OK to me, more generally if it works and it does what is supposed to do, it is enough, it is not "programming" where a (good) programmer may want to rewrite tens of line of code to use a more efficient algorithm, shaving off a few CPU cycles or a few bytes of memory, it is a simple script for Netwatch.

A more experienced member might still be able to suggest some possible betterings or other approaches.

About "/ip fire conn" everyone has his/her own style, personally shortening commands (while very useful when typing on the console) is "ugly" in scripts.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: simple double if / else error  [SOLVED]

Fri Mar 15, 2024 5:42 pm

but it seems OK to me
:local var [:put <etc>] is like correct????


Not tested, for obvious reasons, but must be like this:

untested example code

/tool netwatch
    :local v1 [get [find where host=8.8.8.8       ] status]
    :local v2 [get [find where host=1.1.1.1       ] status]
    :local v3 [get [find where host=208.67.220.220] status]

    :if (($v1 = "down") and ($v2 = "down") and ($v3 = "down")) do={ 
        :log error "WAN1 DOWN"
        /ip route
            disable [find where comment="WAN1" and disabled=no] ; # for skip useless writes, disable only if enabled...
        /ip firewall connection
            :foreach idc in=[find where timeout>60] do={ remove [find where .id=$idc] }
    } else={
        /ip route
            enable [find where comment="WAN1" and disabled=yes]
    }
 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: simple double if / else error

Fri Mar 15, 2024 8:05 pm

A more experienced member might still be able to suggest some possible betterings or other approaches.
I would say:
https://i.chzbgr.com/full/9096072704/hE ... complished
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3505
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: simple double if / else error

Fri Mar 15, 2024 8:28 pm

but it seems OK to me
:local var [:put <etc>] is like correct????
LOL – I thought the same thing. While not a "scripting best practice" for sure, but :put should still return "true", as a string type. And OP has string "true" matchers already, not just a boolean =true. Now that "do ={" would cause troubles however...

One note: If you do test at these scripts at CLI, the entire script needs to be surrounded by { } otherwise the :local are NOT available to next command at the CLI (each CLI cmd entry get it's only scope for variables). If only using /system/script, /tool/netwatch on-up/down/test, etc., those don't need the other block { } since :local's are scoped to the entire script.
 
aleab
Member Candidate
Member Candidate
Topic Author
Posts: 110
Joined: Sat Sep 22, 2018 6:13 pm

Re: simple double if / else error

Fri Mar 15, 2024 8:53 pm

big thank you
@rextended

i know about my limit...
sorry if sometimes i ask...

i test and be sure works fine :)

thank you again
 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: simple double if / else error

Sat Mar 16, 2024 12:04 pm

...and ...:
viewtopic.php?t=103812#p1060385
/ip firewall connection print where (timeout>60) [remove $".id"]
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: simple double if / else error

Sat Mar 16, 2024 7:53 pm


You remember all the links to what I wrote on the forum better than me... ;)
 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: simple double if / else error

Sun Mar 17, 2024 11:34 am

You remember all the links to what I wrote on the forum better than me... ;)
I just follow the crumbs of knowledge that you (and others) spread around all over the forum and try to put them as together as possible, it is intrinsic to the way a forum works that the info is scattered in multiple threads often difficult to find.

Who is online

Users browsing this forum: No registered users and 14 guests