Archive for the ‘Mikrotik’ Category

Mikrotik Netwatch ENHANCED!

Sunday, July 17th, 2016

Mikrotik has a great feature of NetWatch where you can ping something and if it is down you can run a script and if it comes back up you can run another script…

The problem is that sometimes you can lose only a ping or two and you would like your Mikrotik’s NetWatch to wait for a bit longer…

Becouse of that I have written a script that only after X seconds fires the configuration change and also checks if there is another script running (and it stops the execution).

So in Tools/Netwatch I have configured:
001
002
003
004
And now let’s go to the scripts:
005
006
007
008
And if we check the log how it works:

Example when NetWatch IP is down for more than 10 seconds and example wenh NetWatch IP is up for more than 10 seconds…

009

And here the example when during check something happens – so when IP goes down just for a couple of seconds and then returns  – and the second example when it is offline and it just comes up for a couple of seconds..

010

Scripts:

OnWlanUp:

:foreach j in=[/system script job find] do={:if ([/system script job get $j value-name=script] = “OnWlanDown”) do={/system script job remove $j;}}
:local countup value=0;
:local WLANSSID “wlan.novagorica.eu”;
:local SSIDUP [/interface wireless get “01-wlan.novagorica.eu” ssid];
:if ($SSIDUP != $WLANSSID) do={
:while (($countup < 10) && ([/ping address=10.10.0.1 interval=1 count=1]=1)) do={:set countup value=($countup+1); :delay 1000ms; :log warning message=”wlan.novagorica.eu – if UP check $countup”;};
:if ($countup=10) do={/interface wireless set “01-wlan.novagorica.eu” ssid=”wlan.novagorica.eu”; /interface wireless set “01-wlan.novagorica.eu” security-profile=01-wlan.novagorica.eu-noauth ; :log warning message=”wlan.novagorica.eu – gateway up!”;};
} else {:log warning message=”wlan.novagorica.eu – gateway up! – no need to change SSID”;}

onWlanDown:

:foreach i in=[/system script job find] do={:if ([/system script job get $i value-name=script] = “OnUp”) do={/system script job remove $i;}}
:local countdown value=0;
:local WLANSSID “wlan.novagorica.eu”;
:local SSIDUP [/interface wireless get “01-wlan.novagorica.eu” ssid];
:if ($SSIDUP = $WLANSSID) do={
:while (($countdown < 10) && ([/ping address=10.10.0.1 interval=1 count=1]=0)) do={:set countdown value=($countdown+1); :delay 1000ms; :log error message=”wlan.novagorica.eu – if DOWN check $countdown”;}; :if ($countdown=10) do={/interface wireless set “01-wlan.novagorica.eu” ssid=”wlan.novagorica.eu-offline”; /interface wireless set “01-wlan.novagorica.eu” security-profile=01-wlan.novagorica.eu-offline; :log error message=”wlan.novagorica.eu gateway down!”;};
} else {:log error message=”wlan.novagorica.eu – gateway still down! – no need to change SSID”;}