Skip to content

Instantly share code, notes, and snippets.

@highel
Created October 21, 2019 08:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save highel/140409e5d3c145a56f3ff8606a184f7c to your computer and use it in GitHub Desktop.
Save highel/140409e5d3c145a56f3ff8606a184f7c to your computer and use it in GitHub Desktop.
:if ( [/ping 8.8.8.8 count=5 size=64 interval=2s ] =0) do={
:log error "-----No ping";
/system routerboard usb power-reset duration=2;
:log error "----Reset USB Power";
/delay 30s
:log error "disable";
/interface disable lte1;
/delay 5s
:log error "enable";
/interface enable lte1;
}
@highel
Copy link
Author

highel commented Oct 21, 2019

This script restarts 4g lte modem in mikrotik router if several pings are not successful.
Normally it should be scheduled to run about every 5 or 10 minutes

@redeltaglio
Copy link

:if ( [/ping 8.8.8.8 count=5 size=64 interval=2s ] =0) do={

/interface lte at-chat lte1 input="AT+reset"
:local continue true
:local counter 0
:while ($continue) do={
	:delay delay-time=180 ; 
	:if ([/ping 8.8.4.4 count=5 size=64 interval=2s]=0) do={ 
		:set $counter ($counter + 1); 
		:if ($counter>5) do={	
			:set counter 0 
			/sys reboot
		} else={
			/interface lte at-chat lte1 input="AT+reset"
			:delay delay-time=180 ; 
		} else={
			:set $continue false 
			:set $counter 0
		}
	}
}

}

@Tommasov
Copy link

It says ERROR: please specify bus you want to reset [1..2]
So i specified bus=2
then it says ERROR: pcie card detected
My modem is RG502Q-EA

Any workaround?

@seberm
Copy link

seberm commented Feb 16, 2024

# External network connection watchdog
#
# If device gets 10 unsuccessfull pings to 8.8.8.8, it will try to restart the
# LTE modem and its PPoe interface - multiple times (~26x), until the
# rebootThreshold is met. Then the device reboots completely (approximately after 1 hour).
#
# How to kill the routeros script/job?
# /system/script/job/print
# /system/script/job/remove <N>
#
# No not forget to automatically start the script after device boot:
# /system/schedule add name=script-watchdog on-event="/system/script/run \"watchdog-device-and-usb\"" start-time=startup interval=0 disabled=no

# Global do-while loop condition variable - it's possible to stop whole script by setting this variable to "true" in the console.
:global WatchdogStopScript false;

# USB-Reset duration time
:global powerResetTime "10s";

# If connection problems persist and the (pingCount % rebootThreshold)==0, the device reboots.
:global rebootThreshold 26;  # Approximately after one hour

# Threshold when the device tries to USB-reset the LTE modem and its PPoe interface.
:local pingThreshold 10;
:local pingCount 0;

# Name of PPoe LTE interface
:local pppInterface "ppp-lte";

# Ping address
#:local pingAddress "1.2.3.4";   # Just for testing
:local pingAddress "8.8.8.8";

# At first, wait until the device boots up and everything (especially LTE modem) will try to initialize
:log warning "Script: Running watchdog script - waiting on initial delay ...";
:delay 300s;

:do {
    # Every 30 seconds check the connection, reset or increment the pingCount if connection fails.
    :delay 30s;

    :if ([/ping count=1 address=$pingAddress size=64] = 0) do={
        :set pingCount ($pingCount + 1);
        :log warning "Script: Unable to ping the $pingAddress, incrementing the ping counter: $pingCount";
    } else={
        # Reset the ping counter
        :set pingCount 0;
        :log info "Script: Resetting the ping counter: $pingCount";
    }

    :if ($pingCount >= $pingThreshold) do={
        :log error "Script: Network failure detected. Resetting USB and PPoe interface ...";

        /interface disable $pppInterface;
        :delay 5s;

        /system routerboard usb power-reset duration=$powerResetTime;
        :delay $powerResetTime;
        :log warning "Script: USB Power reset complete.";

        # Wait for the LTE modem to boot up
        :delay 60s;

        /interface enable $pppInterface;

        # Wait for the PPoe interface to initialize
        :delay 120s;
        :log warning "Script: Testing if device got the network after USB reset";

        :if ([/ping address=$pingAddress count=1 size=64] = 0) do={
            :log warning "Script: Network still not available after USB/modem reset. Checking reboot threshold: [pingCount=$pingCount, rebootThreshold=$rebootThreshold]";

            :if (($pingCount % $rebootThreshold) = 0) do={
                :log warning "Script: Rebooting device due to prolonged network failure (reached the reboot threshold).";
                /system reboot;
            }
        } else={
            :log info "Network connectivity restored after USB reset. Resetting the ping counter.";
            :set pingCount 0;
        }
    }
} while=( !$WatchdogStopScript );

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