jason@home:~$

Script to check internet connectivity

Tags: Bash

Thanks to some connectivity issues I’m having with my Comcast network connection, I wrote this little script to log the issue. It just tries to grab googles index page every minute and logs it.

#!/bin/bash

WGET="/usr/bin/wget"

while :
do
  $WGET -q --tries=20 --timeout=10 http://www.google.com -O /tmp/google.idx &> /dev/null
  if [ ! -s /tmp/google.idx ]
  then
    echo "`date` Not Connected..!"
    else
    echo "`date` Connected..!"
    fi
    sleep 60
done

I added a second script to calculate the percentage of failure from the log file.

#!/bin/bash

calc(){ awk "BEGIN { print $* }"; }
NUM=`tail -60 connection.log | grep Not | wc -l | cut -f1 -d' '`
DEN=`tail -60 connection.log | wc -l | cut -f1 -d' '`
echo "$NUM / $DEN"
calc $NUM/$DEN