This post details my approach to automating the reboot of my home cable modem. Up until recently my ISP has been extremely reliable and in over 14 years I’ve been a customer with them I could count on my hand the number of times I’d experienced issues. For the last week the service has been very intermittent with disconnects requiring a reboot of the cable modem to restore the connection. My modem is connected to an outlet on an APC PDU. This gives me the ability to power cycle the modem remotely (It’s in the garage).
That’s fine when I’m at home connected to the LAN but what if I’m away from home? On some days, the modem needed a reboot every 20 minutes or so. Logging onto the web GUI of the PDU and manually rebooting the outlet started to become an irritation.
Automating the reboot of the cable modem
The APC PDU can be managed via a telnet connection. I wrote a bash script executed via a cron job which detects if the internet is reachable. If the internet is down the bash script calls an expect script which connects to the PDU via telnet and reboots the outlet connected to the cable modem. I’d like to acknowledge Greg Kitson who published an expect script to Git Hub which saved me a lot of time writing the script I needed 🙂
Detecting if the internet is reachable
The script needed to be up and running quickly so initially ping is used to test reach ability to 126.96.36.199. Sending 10 pings and as long as at least one ping is successful this would be considered as available. I didn’t want to go to aggressive with the reach ability testing due to the odd pings lost here and there which is expected.
A smarter approach to detecting availability
When time becomes available I’ll modify the script to use cURL instead of ping. I’ll connect to maybe three highly available web sites i.e. Google, BBC etc then only consider the internet as down should all three sites fail to respond. This will prevent the script from power cycling the modem unnecessarily due to the ping responses failing. Ping is not the best tool for reliable availability testing.
Scheduling the scripts
The scripts execute from a Ubuntu 16.04 VM and a cron job to kicks of the script every 15 minutes. That seemed like an acceptable time period to me.
The Internet connectivity issues with my ISP continued for a few days after implementing the script so It’s been a worthwhile project and fun to implement. Hopefully the issues with my ISP will settle back down but should there be an issue with the modem in the future I now have an automated resolution requiring no input from humans to resolve. It will also keep the other users of the home network happy should the internet be down due to the modem which should be back up within 15-20 mins.
Please note that I’m using telnet to connect from my Ubuntu VM in my lab to the PDU. In some production environments this might not be a good idea!
You can find the scripts on Git Hub