Tuesday, 1 October 2013

Basic Linux Shell Scripting Language : A Simple BASH Script to Test Your Internet Connectivity

    Most of the users all over the world make use of Google's Index Page to check whether their Internet connection is working or not.  Many times it is required to check periodically whether the server you are running is connected to internet or not. It is very cumbersome to open the web page every time you wish to check the connection. As an alternative, it definitely makes sense to run some scripts in the background periodically scheduling them using cron.

The bash script that can be used for this purpose is given below:



$WGET -q --tries=20 --timeout=10 http://www.google.com -O /tmp/google.idx &> /dev/null
if [ ! -s /tmp/google.idx ]
    echo "Not Connected..!"
    echo "Connected..!"

This script will try to download Google's Index Page to the file /tmp/google.idx and it will make a maximum of 20 tries to download it. If the file is not empty, it will print "Connected". If there is no internet connectivity, it will try to connect to the said webpage for not more than 10 seconds. If it is unable to download the web page within 10 seconds, it will print "Not Connected..!".

You May Also Like:

1 comment:

  1. This is quite a simple script, a more powerful version can be found in Debian's ifupdown-extra package, see the network-test script (manpage here: http://manpages.debian.net/cgi-bin/man.cgi?query=network-test&sektion=1&apropos=0&manpath=Debian+unstable+sid)