Sunday, May 03, 2009

A script to check network status

A few weeks ago, my ISP was having some issues. Every afternoon at about 3pm, their connection would die. It was quite annoying and their typical issue resolution is to recommend that I power cycle my router and cable modem.

To speed through the issue logging, I wrote a small script to ping my router, Comcast's gateway and their Domain Name Server. It's nothing big, but I thought I'd share it. I meant to do this a few weeks ago, but neglected to. I lost many files (more on that later) and I needed to reconstruct the script, as I find it to be useful.

My wife recently got a new Vista laptop and I modified it for her. Windows batch scripting has some differences, so it wasn't a 1 for 1 conversion.

Here is the shell script - copy it to your favorite text editor and make it executable. You can change the ROUTER, GATEWAY and DNS variables to which ever ones you would like to use to check.




#!/bin/sh
#set-x

## Network status checking script

### configuration

ROUTER=192.168.1.1
GATEWAY=24.17.108.1
DNS=68.87.69.146

TIMEOUT=20
COUNT=4

### Starting Checks

echo
echo "Starting Network Checks"

echo ".....Checking Router for $TIMEOUT seconds"
ping -c $COUNT -W $TIMEOUT $ROUTER > /dev/null
ROUTERCHK=$?

echo ".....Checking Gateway for $TIMEOUT seconds"
ping -c $COUNT -W $TIMEOUT $GATEWAY > /dev/null
GATECHK=$?

echo ".....Checking DNS for $TIMEOUT seconds"
ping -c $COUNT -W $TIMEOUT $DNS > /dev/null
DNSCHK=$?

echo "Network checks finshied"
echo

### Reporting output

if [ $ROUTERCHK -ne 0 ]
then
echo "The router needs to be reset. Unplug it the power, wait 30 seconds and plug it back in."
else
echo "Router responds okay"
fi

if [ $GATECHK -ne 0 ]
then
echo "There is a problem with Comcast. Power cycle the Cable Modem and try again."
echo "If you are seeing this message again, call Comcast for a credit 800 COMCAST."
else
echo "Gateway responds okay"
fi

if [ $DNSCHK -ne 0 ]
then
echo "There is a problem with Comcast. Call Comcast for a credit 800 COMCAST."
else
echo "DNS repsonds okay"
fi



exit 0



For those of you using Windows Vista (Vista seems to have different ping arguments than XP does), here is a similar script. Again, copy it to your favorite text editor and make it executable. You can change the ROUTER, GATEWAY and DNS variables to which ever ones you would like to use to check.


@echo off

echo.
echo Starting Network Checks

SET ROUTER=192.168.1.1
SET GATEWAY=24.17.108.1
SET DNS=68.87.69.146

SET TIMEOUT=20
SET COUNT=4
echo.
echo .....Checking Router for %COUNT% responses
SET CONNECT=an
ping -n %COUNT% %ROUTER% | find "TTL" > nul
IF ERRORLEVEL 1 SET CONNECT=NO
ECHO You have %CONNECT% active connection to the Router

echo.
echo .....Checking DNS for %COUNT% responses
SET CONNECT=an
ping -n %COUNT% %GATEWAY% | find "TTL" > nul
IF ERRORLEVEL 1 SET CONNECT=NO
ECHO You have %CONNECT% active connection to Comcast's Gateway

echo.
echo .....Checking DNS for %COUNT% responses
SET CONNECT=an
ping -n %COUNT% %DNS% | find "TTL" > nul
IF ERRORLEVEL 1 SET CONNECT=NO
ECHO You have %CONNECT% active connection to the Domain Name Server


echo.
ECHO *****CORRECTIVE ACTION, IF NEEDED*******
ECHO If the Router is not connected, check your wifi connection, then power cycle the router
ECHO If the connection to the Gateway or Domain Name Server is down, power cycle the cable modem
ECHO and repeat the test.
ECHO If it is still down, call Comcast to report the problem and request a credit at 800 COMCAST
echo.
ECHO Power cycle = unplug the device for 30 seconds from AC power, plug it back in and then
ECHO wait 1 minute for it to reconnect.
echo.
ECHO I love you Murph

Enjoy!

5 comments:

Mark said...

That's a lovely little piece of code! Our ISP occasionally wigs out, and I figured there had to be some command line tool to figure out what was going on. It didn't even occur to me that I could use something as simple as ping. Thanks!

Beau said...

Cool! I'm glad you like it. And whats neat about it, it will run on mac's too!

I'm glad the first comment I got was "its cool" rather than someone else commenting about how geeky I was!

Thanks

Unknown said...

Okay, I'll bite. Totally a geek, my love.

Anonymous said...

You kind of lost me at @echo off

Amelia

Beau said...

Murph - I hope that everybody who decides to use it keeps the "I love Murph" line

Amelia - well thats alright, you got through the unix script with out question and it was the windoze script that threw you for a loop! :D