Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there an easy way to check Internet connectivity from console? I am trying to play around in a shell script. One idea I seem is to wget --spider http://www.google.co.in/ and check the HTTP response code to interpret if the Internet connection is working fine. But I think there must be easy way without the need of checking a site that never crash ;)

Edit: Seems like there can be a lot of factors which can be individually examined, good thing. My intention at the moment is to check if my blog is down. I have setup cron to check it every minute. For this, I am checking the HTTP response code of wget --spider to my blog. If its not 200, it notifies me (I believe this will be better than just pinging it, as the site may under be heavy load and may be timing out or respond very late). Now yesterday, there was some problem with my Internet. LAN was connected fine but just I couldn't access any site. So I keep on getting notifications as the script couldn't find 200 in the wget response. Now I want to make sure that it displays me notification when I do have internet connectivity.

So, checking for DNS and LAN connectivity is a bit overkill for me as I don't have that much specific need to figure out what problem it is. So what do you suggest how I do it?

Here is my script to keep checking downtime for my blog:

#!/bin/bash

# Sending the output of the wget in a variable and not what wget fetches
RESULT=`wget --spider http://blog.ashfame.com 2>&1`
FLAG=0

# Traverse the string considering it as an array of words
for x in $RESULT; do
    if [ "$x" = '200' ]; then
        FLAG=1 # This means all good
    fi
done

if [ $FLAG -eq '0' ]; then
    # A good point is to check if the internet is working or not
        # Check if we have internet connectivity by some other site
        RESULT=`wget --spider http://www.facebook.com 2>&1`
        for x in $RESULT; do
            if [ "$x" = '200' ]; then
                FLAG=1 # This means we do have internet connectivity and the blog is actually down
            fi
        done

    if [ $FLAG -eq '1' ]; then
        DISPLAY=:0 notify-send -t 2000 -i /home/ashfame/Dropbox/Ubuntu/icons/network-idle.png "Downtime Alert!" "http://blog.ashfame.com/ is down."         
    fi  
fi

exit

This way I need to check for internet connectivity only where there is an issue with my blog response code. Its a bit heavy (as I am not using ping) but should not give any false positives. Right? Also how can I randomize pinging to a different site everytime, like facebook, google, yahoo etc. Also (I was trying to avoid any I/O) I can write to a log file by which I can check the count of downtime checks and then skip further checks till the site is down or cause longer checks (10mins instead of every min). What do you think?

share|improve this question
    
"Internet connectivity" does not have a clear definition, you can only test the connectivity to a specific service or set of services. If you mean "LAN" connectivity a good option is to ping your network gateway. –  João Pinto Feb 24 '11 at 23:43
    
In layman terms, I would define Internet connectivity is if you can access internet. Problem can by anything in between. I just need to differentiate between if its working fine or there is problem out of the set of problem that can be responsible. I hope I made myself clear. :) –  Ashfame Feb 25 '11 at 6:28
add comment

5 Answers

up vote 8 down vote accepted

Checking whether specific website is up

First, there is multiple good online monitoring services. To pick one, Pingdom includes free account for monitoring one target. (Disclaimer: I am not affiliated with Pingdom in any way).

Second, using wget --spider for your own script is good idea. You'll get some false positives when your computer is down, or when your DNS server is not working. Checking return code is straightforward way to do it.

wget --spider http://example.com
if [ "$?" != 0 ]; then
  echo "Website failed!" | mail -s "Website down" your_email@provider.tld
fi

Yet again, there is holes in this approach. If your provider has cached your DNS record, but DNS server is down, others can't access your site, even though monitoring says everything is fine. You can write short workaround with host, for example host example.com <your dns server IP>. That will return error, if DNS server is not responding, even if OpenDNS or your own provider's DNS server works fine.

Checking whether internet is working

There isn't really easy way in general way.

You can for example run ping -c1 multiple well known sites (for example www.google.com, facebook.com and ping.funet.fi) and check return codes to determine whether any of these is reachable. You can automatically check return code by using variable $?. Parameter -c1 is limiting number of ping packets to one.

You may encounter problems with some public wifi networks when there is login gateway that redirects all ping requests and HTTP requests. If so, you may get ping responses and correct HTTP status codes, even when you can't access any other sites.

If you want to check cable state, you can use

sudo ethtool eth0

From output (excerpt):

Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
Link detected: yes

However, this is not telling whether you really have connectivity or not, just whether cable is connected and something is on other end.

share|improve this answer
    
This is totally for my setup, working on Always-ON Ethernet. But glad that you pointed out how it won't work correctly in some cases. Also I believe pinging would be faster than wget. What do you think? and if I am checking for a 200 status code anywhere, I would rather be just pinging it. Right? or there is something that I should know? –  Ashfame Feb 25 '11 at 6:39
1  
ping is faster, and better, because other end won't be angry for you. ping is pretty safe, but even that do not guarantee that you can open web pages. Also, it is possible that firewall is blocking ping but allowing HTTP requests. –  Olli Feb 25 '11 at 7:18
1  
Pinging a well known dns server like 4.2.2.2 is also useful in case you have net connectivity but broken dns. –  djeikyb Feb 25 '11 at 8:56
    
For DNS checking you can of course use host (DNS query). It's very fast also. –  Olli Feb 25 '11 at 9:38
    
Thanks for your response @Olli & @djeikyb I have updated the question, please take a look –  Ashfame Feb 25 '11 at 12:57
show 2 more comments

Checking to see if a site is up is usually done with a monitoring tool like nagios. This will continuously monitor the site, and can notify you of outages.

When checking if the Internet is up from the command line I run through a number of steps:

  • Check Internet is up ping google.com (checks DNS and known reachable site).
  • Check web site is up use wget or w3m to fetch page.

If Internet is not up diagnose outward.

  • Check gateway is pingable. (Check ifconfig for gateway address.)
  • Check DNS servers are pingable. (Check /etc/resolv.conf for addresses.)
  • Check to see if firewall is blocking. (Check /var/log/syslog as I log blocks.)

If Internet is up but site is down check with w3m isupme/example.com replacing example.com with the site that appears down. Use wget, lynx, or which ever command line browser you have available if you don't have the w3m browser installed.

share|improve this answer
    
Thanks for your suggestion! Does my script covers the basic requirement of checking and alerting me when my site is down? And it will be quiet in case there is some Internet connectivity problem –  Ashfame Feb 27 '11 at 16:35
    
It looks like your script will do what you want. You could include the wget command in the if statement as if ! wget --spider http:///example.com; then. –  BillThor Feb 27 '11 at 16:58
    
you mean wget always return 1 for 200 HTTP response code? –  Ashfame Feb 27 '11 at 20:08
    
I mean the test [ $? != 0 ] is the the same as the test ! command. wget is documented to return 0 on success and not 0 for failure. The if behaves identically which ever way you code it. If wget returns failure on a 200 response, the test will fail either way your write it. The same if it returns 0 for a response code other than 200. –  BillThor Feb 27 '11 at 23:25
    
Thanks! That would make the script smaller and easy to understand. –  Ashfame Feb 28 '11 at 5:24
add comment

This is the shell srcript I'm using to test for internet connectivity:

alarm.sh

#! /bin/bash

if curl --silent --head http://www.google.com/  |egrep "20[0-9] Found|30[0-9] Found" >/dev/null
then
echo Internet status: OK

else
echo Internet status: ERROR
mpg321 alarm.mp3 &> /dev/null

fi
sleep 60
clear

./alarm.sh

You will need to install curl and mpg321

sudo apt-get install curl mpg321

You will need a .mp3 sound file renamed to alarm.mp3 in the same folder if you want audible alarm functionality. Finally configure website URL and egrep to your needs.

share|improve this answer
add comment

Checking whether internet is working

sudo nm-tool | grep "State: connected" | wc -l

From output (excerpt):

1 #System is connected to internet

However, as mentioned by others, this is not telling whether you really have connectivity or not. Eg. You could be connected to the router and router is not necessarily connected to internet.

share|improve this answer
add comment

I'm using this method...

if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
    echo Internet isn't present
    exit 1
else
     wget www.site.com
fi
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.