December 13, 2024

If you have been following the Linux shell scripting primer so far, you would have seen that most of the things that go in a shell script are the daily Linux Terminal commands.

Here is a demonstration of one such command :

Let us have a shell script which will ping a specific standard website and let us know if it is reachable or not. In fact you can use this as a basic test to check for internet connectivity.

Shell script named pingsite.sh as below :

#!/bin/sh
# Demonstrated for fun by guys at ihaveapc.com
# Ping a standard website with output suppressed, if ping completes then display success else failure

echo "Checking internet connectivity..."
ping -c 5 www.google.com>>/dev/null

if [ $? -eq  0 ]
then
echo "Able to reach internet, yay!"
else
echo " Not able to check internet connectivity!"
fi

What the above script does is ping google.com for a count of 5 but suppresses the output by redirecting it to a dummy file which is /dev/null so that it is not displayed on the Terminal.

Make the script executable first :

avp@avp-mint ~/scripts $ chmod a+x pingsite.sh

Here is what the output of the executed script looks like when online :

avp@avp-mint ~/scripts $ ./pingsite.sh
Checking internet connectivity...
Able to reach internet, yay!

Output when offline :

avp@avp-mint ~/scripts $ ./pingsite.sh
Checking internet connectivity...
ping: unknown host www.google.com
Not able to check internet connectivity!

[The simpler version of the above is to simply put the ping command with a standard website in a script and run it, that too will be ok as all you need to know is whether pinging major websites turns ok .]

Isn’t it awesome to automate daily or recurring mundane tasks using such tiny Linux shell scripts ?

Cheers.

By admin

Related Post

4 thoughts on “A Simple Linux Shell Script To Test Internet Connectivity”
  1. Hi,

    I have not idea about how to program a bash script, but I’m interesting in an script able to check the connectivity to internet, and take a decision of what route to internet should be used.
    In my case I run 2 differentes scripts to select one or another route to internet for my LAN.
    So, the script I need should be enough smart to know what route I’m currently using, check for a public internet IP, and if the connectivity is down change execute the script to redirect connections to the other route.

    Any help will be appreciate.

    Thank you!

    1. Argie,

      Assuming you are referring to multiple ISPs, you can note what is the default gateway and the IP assigned when using each of these connections (using ipconfig), then say each of these are identified, they won’t be pingable by the other ISP or that the network block assigned will be different. That can be used to identify the current ISP. Besides that, you can note the DNS server IPs too as each ISP will usually have it’s own and won’t be reachable by any network which is not it’s own.

      Also, say when using gateway 1 for ISP1, if that is not pingable, time to move to ISP2 and so on. Once that is done, a simple ping to internet sites like google should suffice to make sure connection is working as expected.

      For getting to know the public IP directly and putting in your script, http://www.icanhazip.com can be used as it only shows public IP as text.

      Hope this helps a bit.

      Cheers,
      ihaveapc.com guys

  2. Hi Lab Rat,

    The $? checks for the command status, so if it returns 0 that means ping was ok else the error message.

    You can try this : type any valid command in Terminal and when it completes successfully type : echo $?. It will display 0.

    Hope this helps.

    Cheers.

  3. Ok this is a really cool script especially for Linux newbie like me.

    Can you tell me whats the $? for pls ?

    – Lab Rat

Comments are closed.