Here is a script & netwatch to auto reboot if/when you can’t ping something
NOTES
:
Do this at your own risk.
I am not responsible for anything you do that breaks your nework.
Notes about this set of scripts :
** I have tested this on Mikrotik ROS version 7.18.2 ( I have not tested this on anything older than 7.18.2 )
There are two parts to this set of scripts
- a /system script
- a /tool netwatch script
— You will need and use both of them for this to work properly
Instructions ; modify the following for your environment in the tool-netwatch section:
- addresstoping 192.0.2.254 ( globally change all 192.0.2.254 to be an IP address you want to check in/on your network
- maxcounter 120 ( optional - change this for how may test pings will fail b4 you want a reboot )
- sleepseconds 5 ( optional - how long to wait during ping failure to send additional test pings )
( Note - total seconds for an auto reboot when something does not ping is ((( maxcounter * sleepseconds ))) - in this case 120-seconds times 5-seconds = 600-seconds -aka- 10-minutes ).
I keep the reboot delay long enough so that nothing happens when you are upgrading a man-in-the-middle device and allows upgrades & reboots . - Optional – you can modify what these scripts put in your log , just look for lines that begin with → log error or log warning
Logs in Blue are good things
Logs in Red are bad things
– Now to put in this set of scripts
Open a telnet or ssh or winbox terminal ( you want to be in the CLI ). Then just copy and paste
/system script
add dont-require-permissions=yes name=RebootScript owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=“/system reboot\r
\n/system reboot\r
\n#”
add dont-require-permissions=yes name=NetwatchRebootedLog owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=“/file print file=flash/NetwatchRebootedLog\r
\n/file set NetwatchRebootedLog.txt contents="NetwatchRebootedLog.txt - Check the date this file was created to find the list Netwatch performed a reboot"”
add dont-require-permissions=yes name=all-reset-mac-address owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=“foreach i in=([/interface ethernet find]) do={\r
\nlocal cmd ("/interface ethernet reset-mac-address " . $i)\r
\n:execute $cmd\r
\n}”
/tool netwatch
add comment=“If-Can’t-Ping-192.0.2.254-then-reboot” disabled=no down-script=“# 2025-04-17-0830\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 "* DOWN - NetWatch -----> 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 "* DOWN - NetWatch - NotConnected ----->ping to $addresstoping failed on attempt $counter of $maxcounter – Will try again in $sleepseconds seconds"\r
\n} else {\r
\n:log warning "* RECOVERED - NetWatch - Connected -----> ping success on to $addresstoping attempt $counter of $maxcounter <----- No Further testing needed — Program will exit -----"\r
\n/quit\r
\n/break;\r
\n/break\r
\n#exit\r
\nquit\r
\n#end\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 "* DOWN - NetWatch - I need to Reboot - I will attempt a → /system script run RebootScript — executing reboot script in 10 seconds"\r
\n:delay 10\r
\n/system script run RebootScript\r
\n:delay 10\r
\n:log error "* DOWN - NetWatch - I need to Reboot - I will attempt a → /system reboot — executing reboot script in 10 seconds"\r
\n:delay 10\r
\n/system reboot\r
\n:delay 10\r
\n:log error "* DOWN - NetWatch - WARNING - FAILURE - Netwatch should of rebooted this Mikrotik ****"\r
\n:delay 10\r
\nexit\r
\nquit\r
\nend\r
\n#\r
\n" host=192.0.2.254 http-codes=“” interval=1m name=“If-Can’t-Ping-192.0.2.254-then-reboot” start-delay=1m30s startup-delay=1m30s test-script=“” type=icmp up-script=":log warning " UP - Netwatch WatchDog Ping Check to 192.0.2.254 then reboot if necessary is now Loaded & Running *"”
** If you try or use this - please post a comment and any suggestions
Hmm , anybody notice the second script ( a goodie ). It will reset the mac address on your ethernet faces back to hardware-default. It’s a nice tool script for when you have restored a mikrotik backup made on one mikrotik to a different mikrotik.
North Idaho Tom Jones