I run a couple of servers at home, and generally need access when I'm out and about, and the most annoying thing is when you desperately want access but find that good ol' Virgin Media has kicked you offline. This is normally resolved by just turning the modem off then on again (I run my Superhub in Modem Mode) but is frustrating nevertheless.
So, I wanted an automated solution to this - and thought this might help others with ideas too.
The concept was I wanted the Pi to check for Internet connectivity every 10 minutes. If there was a problem, after testing a few times, to send some commands to essentially switch the modem off and on again.
I bought this Pi-controlled sockets from Amazon made by Energenie. Mine actually arrived with the more expensive controller board which actually needed some C coding rather than Python after a lot of head-scratching. But essentially, follow the PDF they give you to make sample programs and then you can make a script/program to turn a socket on and off from the command line. My C program is here.
So, once you've got your scripts/programmes controlling your socket manually - now we can automate it.
This is the script I've created (from various sources) which I've added to crontab, running every 10 minutes:-
Code: Select all
#!/bin/bash
FirstIP="8.8.8.8" #Google DNS
SecondIP="208.67.222.222" #OpenDNS Public DNS
LogFile=/root/modem_reboot.log
COUNTER=1
while [ $COUNTER -lt 5 ] ## Try 5 times before resetting the modem. Modify as needed.
do
if ping -c 1 -w 5 $FirstIP &> /dev/null ## Determine if first IP-address is reachable.
then
echo "First IP responds, do nothing"
exit 1
elif ping -c 1 -w 5 $SecondIP &> /dev/null ## Determine if second IP-address server is reachable.
then
echo "Second IP responds, do nothing"
exit 1
else
let COUNTER=COUNTER+1
fi
done
#Turn modem off
echo "Turning modem off..."
/root/off
echo "Now waiting 10 seconds"
sleep 10 ## Delay of 10, increase as needed.
#Turn modem back on again
echo "Turning modem back on..."
/root/on
echo "Power cycle complete. Writing to log." ## Append date and timestamp to log
echo "$(date "+%d%m%y %T") : Modem power cycled" >> $LogFile 2>&1Firstly, pick a couple of IPs to check which should always be available. I thought Google's and OpenDNS's primary DNS servers were a good choice. Then choose the location and file you want to write to your logfile. This is handy to check if there's been any rebooting going on (also good for a log to complain to Virgin Media...)
The script then sets up a counter, and tests the first IP, waiting 5 seconds between each ping. If it fails, it tests the second IP. If that fails, it loops another 4 times - just to make sure. If at any stage the Internet is ok, it'll quit out.
Now, if you get through 5 loops of 2 IP tests and still no internet, it'll then call your "off" program. Wait 10 seconds. Then call the "on" program to toggle the power on your modem.
Lastly, writes a timestamp to a log file so you can check when this all happened.
I run the DietPi distro which has the default home directory as /root - so please tweak the script to suit your home directory placement.
I hope that's some use to somebody and/or gives someone an idea to so something similar.
It works great for me, and I also use the Pi as a web server so it's doing more than just checking the 'net!