Shell Script to check internet connection status from Linux

In case you need a shell script that can be used to check your internet connection via console, the following script lines could be your friend. The shell script below will perform a wget connection to google with several tries and then returned the value as online or offline string.


wget -q --tries=10 --timeout=20 --spider
if [[ $? -eq 0 ]]; then
        echo "Online"
        echo "Offline"

If you are new to Shell script, you can follow these steps to create a simple shell script/program. I assume you are able to use nano (actually you may use any other text editor you like such as vi).

Create a new shell script file called


Next, paste those code above to the new file

check internet connection script

Now save the file and exit. CTRL + X is used if you are using nano to edit theĀ file.

Now make the file executable

chmod +x

Finally run the program



[root@gateway ~]# sh
[root@gateway ~]#



Leave a Reply

Your email address will not be published. Required fields are marked *