Skip to content

Instantly share code, notes, and snippets.

@mezhgano
Last active February 27, 2025 11:43
Show Gist options
  • Save mezhgano/2eb4f60926dc09c0e4a0bf8070eb4e9e to your computer and use it in GitHub Desktop.
Save mezhgano/2eb4f60926dc09c0e4a0bf8070eb4e9e to your computer and use it in GitHub Desktop.
Mikrotik (RouterOS) - Reset LTE if no ping
# External network (WAN) connection watchdog
#
# Tested and works in RouterOS 6.49.17 (stable)
#
# If device gets 10 unsuccessfull pings to all ips in $pingAdresses array, it will try to restart the
# LTE modem and its interface - multiple times (RebootThreshold - ResetThreshold), until the
# RebootThreshold is met. Then the device reboots completely.
#
# How to kill the routeros script/job?
# With terminal:
# /system/script/job/print
# /system/script/job/remove <N>
# Or with GUI:
# System > Scripts > Jobs > Select Job and click minus button
#
# Do not forget to automatically start the script after device boot:
# With terminal:
# /system/scheduler add name=WAN_Watchdog on-event=WAN_Watchdog start-time=startup interval=0 disabled=no
# or with GUI:
# System > Scheduler > click plus button and fill the fields accordingly
# Global do-while loop condition variable - it's possible to stop whole script by setting this variable to "false" in the terminal.
:global WANWatchdogEnable true;
# USB-Reset duration time
:global PowerResetTime "10s";
# If connection problems persist and the (PingCount = RebootThreshold) the device reboots.
:global RebootThreshold 15;
#How often to check WAN connectivity
:global CheckInterval "30s";
# Threshold when the device tries to USB-reset the LTE modem and its interface.
:local ResetThreshold 10;
:local PingCount 0;
# Name of PPoe LTE interface
:local WANInterface "lte1";
# Array with well-known DNS addresses to ping.
# It is recommended to choose more than just one address to prevent false detections in case of DNS provider failure.
#
# "77.88.8.8"; # Yandex DNS (1st)
# "77.88.8.1"; # Yandex DNS (2nd)
# "1.1.1.1"; # Cloudflare DNS (1st)
# "1.0.0.1"; # Cloudflare DNS (2nd)
# "8.8.8.8"; # Google DNS (1st)
# "8.8.4.4"; # Google DNS (2nd)
#
# You may use local addresses for debugging purposes
# :global PingAddresses {"192.168.5.1"; "192.168.5.17"; "192.168.5.17"};
:global PingAddresses {"77.88.8.1"; "1.1.1.1"; "8.8.8.8"};
:log info "WAN_Watchdog: PingAddresses: $([:tostr $PingAddresses])";
:local WANConnection do={
:global PingAddresses;
:local Counter 0;
:foreach Address in=$PingAddresses do={
:set Counter ($Counter + 1);
:if [/ping address=$Address count=1 size=64] do={
:return true
} else={
:if ([:len $PingAddresses] = $Counter) do={
:return false
}
}
}
}
# At first, wait until the device boots up and everything (especially LTE modem) will try to initialize
:log info "WAN_Watchdog: Waiting on initial delay...";
:delay 300s;
:do {
# Every $CheckInterval seconds check the connection, reset or increment the PingCount if connection fails.
:delay $CheckInterval;
:if [$WANConnection] do={
# Reset the ping counter
:set PingCount 0;
:log info "WAN_Watchdog: Network is OK.";
} else={
:set PingCount ($PingCount + 1);
:log warning "WAN_Watchdog: Unable to ping all ips from the PingAddresses, incrementing the ping counter: $PingCount";
}
:if ($PingCount >= $ResetThreshold) do={
:log error "WAN_Watchdog: Network failure detected. Resetting USB interface...";
/interface disable $WANInterface;
:delay 5s;
/system routerboard usb power-reset duration=$PowerResetTime;
:delay $PowerResetTime;
:log warning "WAN_Watchdog: USB Power reset complete.";
# Wait for the LTE modem to boot up
:delay 60s;
/interface enable $WANInterface;
# Wait for the LTE interface to initialize
:delay 150s;
:log warning "WAN_Watchdog: Testing if device got the network after USB reset...";
:if [$WANConnection] do={
:log info "WAN_Watchdog: Network connectivity restored after USB reset. Resetting the ping counter.";
:set PingCount 0;
} else={
:log warning "WAN_Watchdog: Network still not available after USB reset. Checking reboot threshold: [PingCount=$PingCount, RebootThreshold=$RebootThreshold]";
:if ($PingCount = $RebootThreshold) do={
:log warning "WAN_Watchdog: Rebooting device due to prolonged network failure (reached the reboot threshold).";
/system reboot;
}
}
}
} while=( $WANWatchdogEnable );
@mezhgano
Copy link
Author

mezhgano commented Oct 3, 2024

This gist was forked from:
https://gist.github.com/highel/140409e5d3c145a56f3ff8606a184f7c?permalink_comment_id=4907004#gistcomment-4907004

And rewritten with using a function (WANConnection) that allows multiple ips being pinged, to prevent false detections in case of DNS provider failure.

Also slightly changes in variable names, comments and defaults, to see exactly what changed, just download both scripts and compare them.

By the way, i'm using VSCode or VSCodium with this extension to get syntax highlighting:
https://marketplace.visualstudio.com/items?itemName=devMike.mikrotik-routeros-script

@ilium007
Copy link

Thanks for the script! Working perfectly on ROS 7.18.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment