Auto Reboot Wired ADSL/ Cable Modem via DDWRT Script on Wireless Router

axeman

Ye Olde Systems Breaker
Skilled
Now, this is a very simple script, that when added to your DDWRT device will keep checking for an internet connection. If it fails to find an active connection, it will reboot the device you decide via Telnet using this script. For unstable connections which experience drops you can change the values highlighted to a larger number.

Intended scenario:

Telephone/Cable -> Standard Router/Modem -> DDWRT Wireless Device -> Your Gadgets.

The DDWRT device will keep monitoring your connection to the internet and in case of drops, simply reboot your modem to try and recover it back.

The values you may play with are underlined for your convenience.





The number in Red is the number of times the script will check for a stable connection.
The number in Blue is the interval in minutes the script will be scheduled (here it is every 10 mins)
The IP Addresses in Black are the ones you would like to 1. Ping , 2. Telnet to.

Replace the username and password with your own (Duh!) and the reboot command if it varies in your model.

---

cat <<END > /tmp/keepalive.sh
#!/bin/sh
ping_attempt=0
while ( ( /bin/ping -c 1 4.2.2.2 | /bin/grep -q '100%' ) && \
[ \$ping_attempt -lt '4' ] )
do
ping_attempt=\`expr \$ping_attempt + 1\`
echo $ping_attempt
/bin/sleep 1
done
if [ \$ping_attempt = 4 ] ; then
(
sleep 5
echo -ne "username\r"

sleep 1
echo -ne "password\r"

sleep 1
echo -ne "reboot\r"

sleep 30
) | telnet 192.168.1.1
fi
END
chmod 755 /tmp/keepalive.sh
echo '*/10 * * * * root /tmp/keepalive.sh' >> /tmp/crontab

---

If your DDWRT device is directly connected to the internet without any router in between, you can remove the section in (..) and the telnet command and replace it fully with a simple "reboot" command.

Finally - you enter this script in the section : Administration - Commands - (paste your edited script) - Save for Startup. Reboot.

Testing: Pull out your telephone cable from the router and wait a 10 minute cycle to see if the script auto reboots your router.

Troubleshooting notes: Make sure you can ping the public IP you have set above, the username and password you have set, telnet should be enabled on your router, certain v23 of DDWRT don't work perfectly with this script, the reboot command is the right one for your router, the time interval you have set, and enable NTP on your DDWRT device. If all else fails, First upgrade your DDWRT build.

Inspiration derived from: Mumbai Monsoon & Mera Telephone Nahin Lagta's (MTNL's) quirky combination.

Please share this post if you find it useful, and do always link back to this thread on TE.
 
I have to ask about the need for such a script at all. Doesn't the modem automatically try to recover the connection and keep doing so until it gets one ? or is there some limit where it just stops and manual intervention ie your script comes in.
 
Back
Top