Tiny bash scripts: check Internet connection availability

Sometimes it is necessary to check whether server you want to run some big bash script is connected to Internet. Usually it makes sense while running scripts periodically using cron.  Below is the tiny bash script for this purpose:

#!/bin/bash

WGET="/usr/bin/wget"

$WGET -q --tries=10 --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null
if [ ! -s /tmp/index.google ];then
	echo "no"
else
	echo "yes"
fi

As you see it tries to download google’s index page, if it’s not empty script returns “yes”, if there is not Internet connection available script will return “no”. If it is impossible to fetch the page in more than 5 seconds script will return “no” as well.

Anything to add? You are welcome! :)

 
 
» You might also be interested in the following articles:
Can’t find usable shell script encryption solution…
CGI Perl scripts debugging (solve 500 Internal Server Error)
Get suspend/hibernate working fast in Ubuntu Feisty Fawn (7.04) and Edgy Eft (6.10)
FAQ: How to install and configure MySQL cluster?
Make linux shell scripts encrypted



» Want to stay up to date? Subscribe to our E-MAIL or RSS feed!

13 Responses to “Tiny bash scripts: check Internet connection availability”


  1. 1 Daniel Holm

    Hi,

    Thank you for that. I have been looking for something like that. In Ubuntu Im using Link Monitor Applet, which pings any selected host.

  2. 2 Dirk Deimeke

    Sorry, that only proves, if you can reach a site named google.com what is not enough for getting checked if internet connection is up and running.

    This will not work, if the google website is down (I saw that once) or your DNS-Server is not reachable.

    It would be better to check, if you can reach any DNS-Server.

  3. 3 Binny V A

    Dirk has a point. I would suggest ping OpenDNS ip address – 208.67.222.222

  4. 4 artiomix

    Thanks for commenting, guys! Absolutely agreed with you that is not a panacea to fetch a google’s main page. Also it’s not an option to ping any public IP for the same reason — what if the firewall accepts icmp traffic only?

    At the same time to make the script more useful you can replace “www.google.com” with “74.125.67.100″ that is google’s public IP… This is to walk-around possible DNS problem. How do you think?

  5. 5 Dirk Deimeke

    Try
    nmap -sU -p
    on some dns-servers. If one of them answers, you are connected.

  6. 6 FloridaLottoWinningNumber

    those are very good tips!

  7. 7 ElDoradoCasinoShreveport

    I will ad this to my tools, thanks for sharing it.

  8. 8 Cris

    Whatever happened to good old ping and tracert? ping -c 4 208.67.222.222 –>> this sends 4 pings (to the opendns IP) ofc, you’ll want to check what other nifty little flags ping has. Careful, sending too many pings to an IP might throw some warning flags in your direction. Remember, be considerate.

  9. 9 Suresh

    That was very helpful! Thank You!

  10. 10 ubuntuverse

    what about to ping first the gateway 192.168.0.1 or something similar and than ping google?

  11. 11 UnderT

    Some ping way:

    #!/bin/sh
    IS=`/bin/ping -c 10 google.com | grep -c “64 bytes”`
    if (test “$IS” -lt “3″) then
    echo FAIL
    else
    echo OK
    fi
    exit

  12. 12 UnderT

    In first variant, after

    echo “yes”

    may be will be coolest to add:

    unlink /tmp/index.google

  13. 13 Be??a?

    ?????????? ??????, ?? ? ??? ???? ? ?????? ????? ???? ?? ????. ????? ???? ?? ?????? ?? ???????, ????? ? ???????? :)

Leave a Reply




Friendly Sites:Who is behind Linux Screw?
Aspiring Sysadmin | GeekyBits³ | Bash Cures Cancer | TOTMS
Linux Operating System | Small Linux Deployments | My SysAd Blog
The Danesh Project | ZEPY | Linux config Wiki | Planet Sysadmin
The Sys Admin | {buhay sysad} | a non-geek's linux notes
Linux HOWTOs, Tutorials & Projects with Adam Palmer | LinuxAlt.Com
My name is Artem Nosulchik (artiomix AT gmail DOT com) and I'm Linux/Unix, Cisco systems engineer. The main idea of Linux Screw is to share relevant knowledge, skills and observations over The Web. Here you can find a lot of information related to different Linux distributions, FreeBSD, IOS as well as a other Open Source around staff. Read more ››