Automatic Reboot WatchDog scripts when connection is lost & remote site-survey script
I have been asked this many times via mikrotik forums and other ISP/WISP forums and direct messages.
So I am posting a working set of script to automatically reboot Mikrotik routers & switches if/when network connectivity to your core Network-Operations-Center is lost.
These scripts will auto-reboot a device if/when they loose IP connectivity to your network ( great for remote wireless customer CPE routers on your WISP network ).
Also , I am including a script which will allow a remote admin to connect to a remote customer CPE wireless Mikrotik router and perform a Site-Survey.
Here is the procedure to install/configure my script(s).
-
On your core route in your NOC , you need to configure a bridge or LoopBack interface , then give it an IP address of 192.0.2.254
you can do something like this on a core Mikrotik router:
/interface bridge
add name=WatchDog
/ip address
add address=192.0.2.254 comment=“WatchDog 192.0.2.254” interface=WatchDog network=192.0.2.254 -
Next , verify your other Mikrotik devices can ping 192.0.2.254
-
Next , we need to create a Mikrotik rsc script which will configure and add the scripts into your client Mikrotik devices.
On your local ( windows ) computer , create/edit a new text document named “TomsWatchDogVer02.rsc”
If you are in the DOS prompt , you can type in “notepad TomsWatchDogVer02.rsc” -
Next cut the lines of text below this point and paste the lines into the TomsWatchDogVer02.rsc file then save.
-
-
-
- start cut below this line - - - -
/tool netwatch
disable [ find where host =“192.0.2.254” ]
remove [ find where host =“192.0.2.254” ]
- start cut below this line - - - -
-
-
/system script
add name=RebootScript owner=admin policy=reboot source=“/system reboot\r
\n/system reboot\r
\n#”
add comment=Wlan1-SiteSurvey name=Wlan1-SiteSurvey owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=
“/interface wireless scan wlan1 duration=90 save-file=ManualSiteSurvey-wlan1-90seconds–txt.rtf”
add comment=Wlan2-SiteSurvey name=Wlan2-SiteSurvey owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=
“/interface wireless scan wlan2 duration=90 save-file=ManualSiteSurvey-wlan2-90seconds–txt.rtf”
/tool netwatch
add comment=TomsNetwatchVersion2-Reboot-NotConnected down-script=“#\r
\n:local addresstoping 192.0.2.254\r
\n:local continue true\r
\n:local counter 0\r
\n:local maxcounter 120\r
\n:local sleepseconds 5\r
\n:local goodpings 0\r
\n#\r
\n:log error "-NetWatchVersion2-Reboot-NotConnected - ----> Tom’s NetWatchVersion2"\r
\n:log error "NetWatchVersion2-Reboot-NotConnected - -----> Ping testing for $maxcounter times every $sleepseconds seconds"\r
\n#\r
\n:while ($continue) do={\r
\n:set counter ($counter + 1)\r
\n:delay $sleepseconds\r
\n#\r
\n:if ([/ping $addresstoping interval=1 count=1] =0) do={\r
\n:log error "NetWatchVersion2-Reboot-NotConnected - ----->ping to $addresstoping failed on attempt $counter of $maxcounter – Will try again in $sleepseconds seconds"\r
\n} else {\r
\n:log warning "NetWatchVersion2-Reboot-NotConnected - -----> ping success on to $addresstoping attempt $counter of $maxcounter <----- No Further testing needed — Program will exit -----"\r
\nexit\r
\nquit\r
\nend\r
\n:set continue false\r
\n:set goodpings ($goodpings +1)\r
\n}\r
\n:if ($counter=$maxcounter) do={:set continue false;}\r
\n}\r
\n#\r
\n:log error "NetWatchVersion2-Reboot-NotConnected - — executing reboot script in 10 seconds"\r
\n:delay 10\r
\n/system script run RebootScript\r
\n:delay 10\r
\n:log error "NetWatchVersion2-Reboot-NotConnected - — I should of rebooted by now - will retry a reboot script againt in 10 seconds"\r
\n:delay 10\r
\n/system script run RebootScript\r
\n:delay 10\r
\nexit\r
\nquit\r
\nend\r
\n#\r
\n” host=192.0.2.254 interval=5m
/system
backup save dont-encrypt=yes name=BackupTJ14
/
/log
warning “tj14import.rsc loaded and installed - warning log message in blue”
info “tj14import.rsc loaded and installed - info log message in black”
error “tj14import.rsc loaded and installed - error log message in red”
/
/
-
-
-
- end cut above this line - - -
-
-
-
Next , now we have a rsc file on our windows work-station.
Open a Winbox connection to any remote customer wireless router and go into Files.
Drag your rsc file to the Files folder on the remote wireless Mikrotik router. -
Next , we will run the install rsc file so that it will auto configure & auto-install the scripts.
Using a terminal on the remote Mikrotik router , type in → /import TomsWatchDogVer02.rsc
This will do the following:
Add a /tool netwatch item
Add three scripts into /system scripts
Make a backup
Add some text to your log
— You are done
Now here is how it works.
Auto-Reboot sequence
Every 5-Minutes the /tool netwatch script will perform a ping to 192.0.2.254
If no ping response , then count-down loop will start for 2 minutes.
If during the count-down loop period ( the next 2 minutes ) there is a ping response from 192.0.2.254 , then the script will abort/exit/quit.
If during the 2 minutes a ping response was not received from 192.0.2.254 , then the /tool netwatch script will auto-reboot the remote Mikrotik router.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Added script to perform a remote wireless client site-survey
Using Winbox on the remote wireless connected Mikrotik router ,
go to /System Scripts
Mouse click on → Wlan1-SiteSurvey , then click on [ Run Script ]
The remote customer wireless router will disconnect and perform a site-survey for 90 seconds.
Then remote customer wireless router will then re-connect to your wireless network.
Winbox into the re-connected remote customer wireless Mikrotik device.
Go to /Files and drag the new site-survey file to your windows computer.
Double click on the site-survey file you just now copied to your computer.
You can now view what you would see if you were actually at the customer location performing a site-survey.
If you have any questions ( such as why I picked the IP address of 192.0.2.254 or other questions ) , please ask.
FYI I have been using these scripts for 10+ years on thousands of Mikrotik devices connected to my networks , the work well.
North Idaho Tom Jones