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! :)

 

144 Comments

 

  1. April 2, 2009  9:01 am by Daniel Holm Reply

    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. April 2, 2009  11:18 am by Dirk Deimeke Reply

    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. April 2, 2009  6:39 pm by Binny V A Reply

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

  4. April 2, 2009  7:55 pm by artiomix Reply

    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. April 5, 2009  6:17 pm by Dirk Deimeke Reply

    Try

    nmap -sU -p

    on some dns-servers. If one of them answers, you are connected.

  6. May 11, 2009  2:59 am by Cris Reply

    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.

  7. June 1, 2009  5:53 pm by Suresh Reply

    That was very helpful! Thank You!

  8. June 18, 2009  2:45 pm by ubuntuverse Reply

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

  9. November 24, 2009  9:33 am by UnderT Reply

    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

  10. November 24, 2009  9:39 am by UnderT Reply

    In first variant, after



    echo "yes"



    may be will be coolest to add:



    unlink /tmp/index.google

  11. January 15, 2010  9:39 pm by Be??a? Reply

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

  12. Pingback : Seagate Dockstar Discussion and Tutorial Thread

  13. October 6, 2010  8:34 pm by Rick Loewer Reply

    One more fantastic blog post! I shared this blog on Facebook - you should add a "like" button to your blogposts. :)

  14. December 24, 2010  10:21 pm by vbulletin forum posting software Reply

    Can I just say what a relief to discover someone who truly knows what theyre talking about on a internet. You genuinely know how to bring an problem to light and make it important. A lot more individuals need to read this and understand this side with the story. I cant believe youre not additional well-known due to the fact you truly have the gift.

  15. January 31, 2011  5:40 pm by max Reply

    Why not use curl and get a ssl enabled site and get the benefit of cert verification on the fly to verify the host as well (so you'r not on a "redirect-all-to-login-page"-dns setup)?


    #!/bin/bash
    RESP=$(curl --silent --connect-timeout 5 --retry 3 --retry-delay 2 --head https://encrypted.google.com/ | grep --extended-regexp --regexp="Set-Cookie: (.*); domain=\.google\.com;")
    if [ -n "$RESP" ]; then
    echo "we have internet"
    else
    echo "no internets"
    fi

  16. February 27, 2011  5:15 am by Girl Games Cooking Games Reply

    Hmmm, I totally think we just shipped a t shirt in that has to do with this subject. That is really funny.

  17. April 18, 2011  4:42 pm by Port Elizabeth Property Reply

    I like the helpful info you provide in your articles. I will bookmark your blog and check again here frequently. I'm quite sure I’ll learn lots of new stuff right here! Best of luck for the next!

  18. April 19, 2011  4:28 am by joggerstroller Reply

    Article quality is good. It can be published as well.

  19. April 19, 2011  11:42 am by top link web Reply

    Naturally I like your website, however you need to test the spelling on quite a few of your posts. A number of them are rife with spelling issues and I find it very bothersome to tell you. Nevertheless I will definitely come again again!

  20. April 20, 2011  2:55 pm by Apartamenty Zakopane Reply

    Hi there, just became aware of your blog site through Search engines, and discovered that it's truly informative. I am gonna watch out for brussels. I’ll appreciate if you keep going this in future. Lots of people will be benefited from your writing. All the best, Apartamenty Zakopane!

  21. April 20, 2011  4:28 pm by debt management Reply

    Hello there. I discovered your website via Google while looking for a comparable topic, your web site got here up. It looks good. I have bookmarked it in my google bookmarks to come back later.

Leave a reply

 

Your email address will not be published.