3G PPP monitor & USB Power Reset Script

Hi Guys,

I’m looking for some assistance regarding a script to reset the USB power, should a 3G modem lose it’s IP for whatever reason.

So, the scripts must monitor the 3G modem (i.e ppp1-out), be it the connection status, an IP, or anything else, and should the modem lose it’s connection/become disconnected, the script must then reset the usb power (/system routerboard usb\ power-reset duration=30), give the modem time to initialize/settle, and then resume monitoring, an added bonus would be if it could then update DNS.

I already have the DNS Update, and Power Reset scripts seperate (see below), so if the monitoring script can call other scripts, that would be just as great.

=======USB RESET=======
/system routerboard usb\ power-reset duration=30

=======DNS UPDATE======
:global host “freedns.afraid.org
:global url “http://freedns.afraid.org/dynamic/update.php?====SOMERANDOMCHARS==
/tool fetch url=$url host=$host

I would sincerely appreciate any assistance or tips in this regard.

Okay, so here I have a crude, but working script:

/interface ppp-client monitor ppp-out1 once do={:if ($status != "connected") do= {:log warning  "PPP Link Down, reset USB"; /system routerboard usb power-reset duration=20}}; :delay 120; /interface ppp-client monitor ppp-out1 once do={:if ($status = "connected") do= { /system script run afraiddnsupdate;}}

The above script, is run via the built-in scheduler, run every x minutes. When the script executes, it checks the status of ‘ppp-out1’, if the status is NOT connected (I’m assuming the device is plugged in and functional, the modem just froze, and all it needs is a reboot) it will create a log entry, and then reset the USB power for 20 seconds . Then the script will wait for 2 minutes, then check ‘ppp-out1’ again, if it’s connected, it will execute the dynamic dns update script.

To just reset the modem, without calling another script:

/interface ppp-client monitor ppp-out1 once do={:if ($status != "connected") do= {:log warning  "PPP Link Down, reset USB"; /system routerboard usb power-reset duration=20}}

As I learn more about scripting, I will implement a more ‘intelligent’ way of monitoring the status of ppp connections.


A slight more elaborate script I made while testing the different variables:

/interface ppp-client monitor ppp-out1 once do={:if ($status != "connected") do= {:log warning  "PPP Link Down (#1)"}}; :delay 30;  /interface ppp-client monitor ppp-out1 once do={:if ($status != "connected") do= {:log warning "PPP Link Down (#2)"}}; :delay 30; /interface ppp-client monitor ppp-out1 once do={:if ($status != "connected") do={:log warning "PPP Link Down, Resetting USB Port"; /system routerboard usb power-reset duration=4}}; :delay 120; /interface ppp-client monitor ppp-out1 once do={:if ($status = "connected") do= { /system script run afraiddnsupdate;}}

In summary: This script will check the interface 3 times, 30 seconds apart, each time creating a log entry, on the third status check, it will reset the modem power, and then check if the interface is connected, if it is, it will run the dns update script. Run this slightly expanded script in the terminal first, it might not work 100%. I pasted it here for educational purposes - I’m hoping this can at least give other guys new to programming a starting point.

you have a good feature of status != connected. i will try to implement it.

here’s mine script:

/tool netwatch
add down-script=":local i 0; {:do {:set i (\$i + 1)} while ((\$i < 5) && ([/ping 159.x.x.x interval=3 count=1]=0))};\r\
    \n:if (\$i=5) do={:log info \"3G modem restart\"; \r\
    \n\t /system script run reset3gmodem }" host=x.x.x.x interval=5m

/system script
add name=reset3gmodem policy=ftp,read,write,policy,winbox,api source=\
    "beep frequency=100 length=0.3\r\
    \ndelay 0.3\r\
    \nbeep frequency=800 length=0.1\r\
    \ndelay 0.3\r\
    \nbeep frequency=100 length=0.3\r\
    \n/system routerboard usb power-reset duration=15\r\
    \ndelay 60"

p.s. i recently bought RB951 (it has a beeper), so i just made some fun with it :slight_smile:


mine script doesn’t work in situations, when modem cant connect back (due to some 3g network problems), so if is down 3g network for some 5-10mins, the modems will stay disconnected.
in other situations (9 of 10) it works great :slight_smile:

Hi f38eb,

I like your script as well, for my next script, I want to combine both interface monitoring, and logical (IP/Host) monitoring, for cases when the modem might be dialled/connected, but it’s not responsive. I believe this can happen when the modem alternates between connection states (GPRS/EDGE/UMTS/HSDPA), since within those connection states, such as HSDPA, there are different power states (Fast Dormancy: http://www.youtube.com/watch?v=O8L50sCY7CI).

Good luck with your scripting! - I’ll keep updating this thread as I create improved ones.

Here is a better aproach for anyone still interested in this

:local PPPInter "rds_3g";
:local count 0;

:while ( $count < 3 )  do={ 
	/interface ppp-client monitor $PPPInter once do={:if ($status != "connected") do= {:set count ($count+1); :log warning  "PPP $PPPInter Link Down (#$count)"; :delay 10; } else={ :set count 4; } };
}
:if ( $count > 0 && $count <4 ) do={ 
	:log warning "PPP Link Down, Resetting USB Port"; 
	/system routerboard usb power-reset duration=4;
}

Schedule and make it run every x days hours etc

Clauu thanks

hey, I have made LTE interface using huawei E3372h, which part of script that i have change it.. tengkyu