Batch Script to reboot cable modem

Filed under: Uncategorized — admin at 4:30 pm on Thursday, February 12, 2009

My Comcast cable modem keeps dropping internet connectivity, so I hacked together a script to monitor my internet connection and reboot the modem when needed.
Hopefully this will solve the problem until Comcast can replace the modem or fix their problems.


REM This script checks to see if I am online, if not, it reboots the cable modem. If it cannot reach the cable modem, it resets the router and then the cable modem.
REM if error code is 0 it pinged successfully.
REM if error code is 1, then there was an error.

:Start
echo at Start
sleep 30

ping -n 1 www.google.com
if %ERRORLEVEL%== 1 goto :ping2
goto :Start

:ping2
sleep 2
ping -n 1 www.yahoo.com
if %ERRORLEVEL%== 1 goto :ping3
goto :Start

:ping3
ping -n 1 www.myspace.com
if %ERRORLEVEL%== 1 goto :ping4
goto :Start

:ping4
ping -n 1 www.live.com
if %ERRORLEVEL%== 1 goto :logerror
goto :Start

:logerror
echo %date% %time% >> %COMPUTERNAME%-internetdown.txt

:recovery
ping -n 1 192.168.100.1
if %ERRORLEVEL%== 1 goto :resetrouter
goto :rebootmodem

:resetrouter
plink -i c:\temp\wheatonrouterkey.ppk -batch -ssh -l root 10.5.20.1
reboot

sleep 90
goto :recovery2

:recovery2
ping -n 1 192.168.100.1
if %ERRORLEVEL%== 1 goto :recovery2

ping -n 1 192.168.100.1
if %ERRORLEVEL%== 1 goto :recovery2

ping -n 1 192.168.100.1
if %ERRORLEVEL%== 1 goto :recovery2

ping -n 1 192.168.100.1
if %ERRORLEVEL%== 1 goto :recovery2

:rebootmodem
sleep 20
wget -g -w 1 -t 0 --retry-connrefused http://192.168.100.1/configdata.html --post-data="FREQ_PLAN=NORTH_AMERICA&US_CHANNEL_ID=6&FREQUENCY_MHZ=555000000&DHCP_SERVER=YES&BUTTON_INPUT=Restart+Cable+Modem"
sleep 60
ping -n 1 www.google.com
if %ERRORLEVEL%== 1 echo %date% %time% still not online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 echo %date% %time% phew online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 goto :start
sleep 60
ping -n 1 www.google.com
if %ERRORLEVEL%== 1 echo %date% %time% still not online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 echo %date% %time% phew online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 goto :start
sleep 60
ping -n 1 www.google.com
if %ERRORLEVEL%== 1 echo %date% %time% still not online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 echo %date% %time% phew online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 goto :start
sleep 60
ping -n 1 www.google.com
if %ERRORLEVEL%== 1 echo %date% %time% still not online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 echo %date% %time% phew online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 goto :start
sleep 60
ping -n 1 www.google.com
if %ERRORLEVEL%== 1 echo %date% %time% still not online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 echo %date% %time% phew online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 goto :start
sleep 60
ping -n 1 www.google.com
if %ERRORLEVEL%== 1 echo %date% %time% still not online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 echo %date% %time% phew online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 goto :start
sleep 60
ping -n 1 www.google.com
if %ERRORLEVEL%== 1 echo %date% %time% still not online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 echo %date% %time% phew online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 goto :start
sleep 60
ping -n 1 www.google.com
if %ERRORLEVEL%== 1 echo %date% %time% still not online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 echo %date% %time% phew online >> %COMPUTERNAME%-internetdown.txt
ping -n 1 www.google.com
if %ERRORLEVEL%== 0 goto :start

goto :Start

Here’s how this script works:

1) Ping a series of websites to check if we have internet access. This way if one particular website is down, or is dropping pings, we still can connect.

2) If we still do not have internet access, log the date/time to a logfile.

3) Check if we can access the cable modem.

4) If we can not, reboot my router. This is done by sshing into my Openwrt router and rebooting it.

5) Otherwise reboot the cable modem.
This is done by using wget to post to a form.

6) Keep checking to see if we are online, write that information to a log file, then restart.

It’s a quick hack of a script, but it demonstrates how to use a batch file to see if a website is online.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>