Community discussions

MikroTik App
 
suporteitanet
just joined
Topic Author
Posts: 19
Joined: Fri Sep 20, 2013 4:01 pm

Script not workin when schedulled, but work in terminal

Tue May 24, 2016 3:47 pm

Hi,

I have two scripts, one to update the default gateway e other to ping in default gateway and reboot case the timeout excedds 60.But the timeout script not workin when is schedulled.

Gateway script
:global defGateway;:foreach i in=[/ip route find] do={:if ([:typeof [:find [/ip route  get $i active] yes]]!="nil" && [:typeof [:find [/ip route  get $i dst-address] 0.0.0.0/0  ]]!="nil" ) do={:set defGateway [/ip route get $i gateway]; }}
Netwatch script (adapted from wiki)
:global i 0; {:do {:set i ($i + 1)} while (($i < 60)  &&  ([/ping $defGateway interval=5 count=1]=0))};:if ($i=60 ) do={:log info "Reboot devido ao Netwatch" ;delay 3s; :foreach i in=[/user find] do={:if ([:typeof [:find [/user get $i name] "infra"]]!="nil") do={/user enable $i }} ;/sys reboot};
Tested in versions 6.13 ,6.24 and 6.34.2.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7198
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Script not workin when schedulled, but work in terminal

Tue May 24, 2016 3:58 pm

Because you need to define used variables in script.
 
suporteitanet
just joined
Topic Author
Posts: 19
Joined: Fri Sep 20, 2013 4:01 pm

Re: Script not workin when schedulled, but work in terminal

Tue May 24, 2016 4:11 pm

Because you need to define used variables in script.
/system script add name=Netwatch policy=ftp,read,write,policy,test,reboot source={:global i 0;:local gat $defGateway ;{:do {:set i ($i + 1)} while (($i < 60)  &&  ([/ping $gat interval=5 count=1]=0))};:if ($i=60 ) do={:log info "Reboot devido ao Netwatch" ;delay 3s; :foreach i in=[/user find] do={:if ([:typeof [:find [/user get $i name] "infra"]]!="nil") do={/user enable $i }} ;/sys reboot};};

I modified , but still without success. The defGateway variable have a ip address.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7198
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Script not workin when schedulled, but work in terminal

Tue May 24, 2016 4:14 pm

You are still using undefined variables
defGateway is undefined.
 
suporteitanet
just joined
Topic Author
Posts: 19
Joined: Fri Sep 20, 2013 4:01 pm

Re: Script not workin when schedulled, but work in terminal

Tue May 31, 2016 7:59 pm

:global cond true;
:global defGateway;
:global j 0;

:while ($cond) do={:foreach i in=[/ip route find] do={:if ([:typeof [:find [/ip route  get $i dst-address] 0.0.0.0/0  ]]!="nil" ) do={:set defGateway [/ip route get $i gateway];:set cond false; }}}

{:do {:set j ($j + 1)} while (($j < 60)  &&  ([/ping $defGateway interval=5 count=1]=0))};:if ($j=60 ) do={:log info "Reboot devido ao Netwatch" ;delay 3s; :foreach i in=[/user find] do={:if ([:typeof [:find [/user get $i name] "infra"]]!="nil") do={/user enable $i }} ;/sys reboot};};
Working now. Thanks.