This script can be scheduled to run at regular intervals to ping a known good host on your 3G wan link.
If the ping fails, a 3G modem reset sequence is attempted.
So far this seems to work fine with my ZTE MF190, Huawei E367,272
Code:
## Modem reset script for 3G PPP Wan links with Email reporting ##
## based on ping reply to a known good host. ##.
## The google dns server at 8.8.8.8 is used ##
## in this example, but you can use any known good host to ping. ##
## take note of your wan interface name! ##
#########################################################
## Script uses PPP Modem Init to send AT command to serial port ##
## Commands are simply fired "blind" at the modem ##
## Example sends the AT+CFUN=4, AT+CFUN=6 command sequence ##
## ZTE MF-190 requires AT+CFUN=4, AT+CFUN=1 sequence or AT+ZRST ##
## Sierra Wireless: AT!RESET ##
## ZTE: AT+ZRST ##
## Huawei: AT+CFUN=4, AT+CFUN=6 sequence ##
## This script can be used with Netwatch or sheduled to run on startup ##
## after a watchdog reboot. ##
## You can adjust the delays and AT commands as needed ##
## Schedule this script at whatever checking interval you require. ##
## Remember to set up Tools - Email settings ##
## Peter James 2012-03-15 ##
## 2012-03-23 Added usb port inactive check with router reboot ##
# Set the 3G wan name #
:local WanName "wan1";
# Define the usb port for this connection #
:local usbPortName "usb1";
:log info "Starting $WanName 3G Down Check";
:local i 0;
:local result 0;
### Define the email address to receive 3G modem reset report ###
:local emailAddressToNotify "your_email_address"
## Define host to check ###
:local testhost "8.8.8.8";
## Define number of pings to check ##
:local PingCount 10;
### Get System Identity ###
:local SystemID [/system identity get name]
## If the usb port is inactive, reboot the router to attempt to fix this ##
:if ([/port get [find name=$usbPortName] inactive]) do={
:log info "USB Port $usbPortName detected as inactive - System Rebooting";
/system reboot;
}
## Skip the ping check if the wan interface is not running ##
:if ([/interface get [find name="$WanName"] running]=true) do={
:do {
:set i ($i + 1);
:if ([/ping $testhost interface="$WanName" interval=3 count=1]=1) do={
:set result ($result+1);
}
} while=($i < $PingCount)
}
# ping has failed PingCount times or ping check was skipped #
:if ($result=0) do={
:log info "Starting $WanName 3G Modem Reset Sequence";
# get its ID number #
:local WanID [/interface ppp-client find name=$WanName];
#/interface ppp-client disable $WanID;#
# Toggle the data channel setting #
/interface ppp-client set data-channel="1" $WanID;
delay 5
/interface ppp-client set data-channel="0" $WanID;
# Send first part of reset sequence #
/interface ppp-client set modem-init="AT+CFUN=4" $WanID;
/interface ppp-client enable $WanID;
# Allow time for Init command to be sent #
delay 5;
# disable again to stop dial attempts #
/interface ppp-client disable $WanID;
# Wait for tx and rx RF circuits to switch off #
delay 5;
# Send second part of reset sequence #
/interface ppp-client set modem-init="AT+CFUN=6" $WanID;
/interface ppp-client enable $WanID;
# Allow time for Init command to be sent #
delay 5;
# disable again to stop dial attempts #
/interface ppp-client disable $WanID;
:log info "waiting for modem reset 30 seconds";
# wait for modem to reset #
delay 30;
/interface ppp-client set modem-init="" $WanID;
# Allow modem to dial again #
/interface ppp-client enable $WanID;
# Modem should now redial and connect #
:log info "waiting to send email 30 seconds";
## wait before trying to send email ##
delay 30;
# Check if interface is now running #
:if ([/interface get [find name="$WanName"] running]=true) do={
/tool e-mail send tls=yes subject="$SystemID $WanName 3G Modem Was Reset" to=$emailAddressToNotify body="$WanName 3G Modem Was Reset"
:log info "Email sent $WanName 3G Modem Was Reset"
:log info "$WanName Modem Reset Sequence Complete";
} else={
:log info "$WanName Interface Not Running - Modem Reset Sequence Failed";
}
}
:log info "$WanName 3G Down Check Complete";
***Edit 2012-03-15:
Script modified to add skipping of the ping check if the wan interface is not running.
Some timings adjusted.
Added a data channel toggle sequence to help with a problem clearing the E272 problem condition.
Added interface running check before attempting to send email.
***Edit 2012-03-16
Some modems may take longer to reset than others, it may be necessary to adjust the "delay" values to get this script to work. Unfortunately there is no handshake with the modem and this is a crude "fire command and hope it works" approach.
***Edit 2012-03-23
What I am finding is that sometimes the RouterBoard cannot communicate with the modem, as the usb port has become inactive. This should only normally happen when the modem is unplugged.
Because it happens with the modem still plugged in, it can only be some error condition.
I have modified the script to include a usb port inactive check, which reboots the router to help recover from this problem.