Add Me!Close Menu Navigation

My technical corner about Linux, Perl, programming, computer networks and network security

Add Me!Open Categories Menu

Basics of writing bash scripts, a simple network tester

Sometimes my internet connection is rubbish. Nowadays this scenario shouldn’t happen ever, but the real life is much brutal, as you know. Especially in Poland. So, my life insists me to do something strange things in my computers and here is an ordinary example.

I wrote my simple script some years ago to check quality of my internet connection. What does it mean here (quality)? So, a standard quality indicator is that the internet connection really works – very important.;-)

Another one – if the internet connection works, I must check quality of a routing. If the connection has as a rule near 50 ms to many hosts and suddenly I have 100 times slower, that indicates something is wrong. I don’t have to check numbers, if pings from my connection have 5 sec delay, I see this in normal use of my computer (watching movies isn’t pleasant then)
First test – I check that if I have working internet connection. In this case I use fping program.

### I use IP addresses here due to eliminate possible problems with a DNS service
IP_LIST = ( 173.194.65.101 64.4.11.37 17.172.224.47 192.150.16.117 )
PROBLEMS=0
TESTS=0
for i in "${HOSTLIST[@]}"
do
 ### How many tests...
 TESTS=`expr $TESTS + 1`
 CHECK=`fping $i | grep "is alive"`
 if [ -z "$CHECK" ];
 then
  ### And how many problems...
  PROBLEMS=`expr $PROBLEMS + 1`
 fi
done

if [ "$TESTS" -eq "$PROBLEMS" ];
then
 ### Problem with an internet connection - do something
 ### For example
 /etc/init.d/network restart
fi

Second test checks routing.

### I assume that I have set $SOMEFILE_LOG
### List of hosts which are always work ;-)
HOSTLIST = ( microsoft.com google.com amazon.com adobe.com )

for i in "${HOSTLIST[@]}"
do
 ping -c5 $i >> $SOMEFILE_LOG
done

Now, my script will do few tests and results will be written to the $SOMEFILE_LOG

 

It’s simple and you may add this to the crontab service. Afterwards, you will have many reports about state of your internet connection.

Leave a Reply

You must be logged in to post a comment.