Script not workin when schedulled, but work in terminal

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.

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.

You are still using undefined variables
defGateway is undefined.

: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.