Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


# Sending the output of the wget in a variable and not what wget fetches
RESULT=`wget --spider 2>&1`

# 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

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 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

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


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
up vote 13 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
if [ "$?" != 0 ]; then
  echo "Website failed!" | mail -s "Website down" your_email@provider.tld

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 <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, and 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
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
Pinging a well known dns server like 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

I'm using this method...

if [[ "$(ping -c 1 | grep '100% packet loss' )" != "" ]]; then
    echo "Internet isn't present"
    exit 1
    echo "Internet is present"
share|improve this answer
I got different results for ping -c 1 and ping -c 1 How is that? – MERose Mar 3 '15 at 13:42

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 (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/ replacing 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:///; 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

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
nm-tool has been removed in Ubuntu 15.04+, but you can use nmcli instead. E.g. [[ $(nmcli -f STATE -t g) = connected ]] – Six Jan 27 at 15:15
@Six nmcli -f STATE -t nm the good thing is being independent of web sites! – Aquarius Power Jan 31 at 2:13

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

#! /bin/bash

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

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

sleep 60


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

I needed help with the same question, and I got my answer this way:

echo $(nm-online) $? connection problems
share|improve this answer

Checking whether Internet is working is not so trivial. ping is ICMP, so it might work even if the web proxy is down. Something similar occurs with DNS if you test with an IP.

Since my Internet connection is unstable I created this script (based on this one) that makes a gentle chord sound to call me, and also uses Ubuntu notify when the Internet connection comes back:


hash play 2>&- || { echo >&2 "I require «play» but it's not installed. Try apt-get install sox. Aborting."; exit 1; }

WGET="`which wget`"
noConnectionMessage="No connection, trying again in $delay seconds...";
connectionMessage="WE HAVE INTERNET! :) Trying again in $delay seconds...";

echo "INTERNET TESTER: Type Ctrl+C to quit";
rm --force /tmp/

function playSound {
    time=1;    # Time played
    if [ `sox --version| grep -oP "v[0-9.]+" | sed "s/[v.]//g"` -lt 1431 ]; then    #analize sox version, if greater than v14.3.1 can use pluck
        play -q -n synth $time sine;
        play -q -n synth $time pluck $1;
        #for i in G4 G4 G4 E4;do play -n synth 0.1 pluck $i repeat 2;done    # You can play with something like this also :) (first three notes from Beethoven 5th symphony)

while [ 1 -eq 1 ]; do
    $WGET -q --tries=10 --timeout=2 $URL -O /tmp/ &> /dev/null
    if [ ! -s /tmp/ ];then
        echo $noConnectionMessage;
        #playSound E2
        sleep 1;
        #~ zenity --warning --text "ADDRESS is back"
        notify-send -i "notification-network-wireless-full" "Connection state:" "$connectionMessage";
        echo $connectionMessage;
        playSound E3
    sleep $delay;

exit 0;
share|improve this answer

I was looking for a script that would continuously test my internet connection, that would be started whenever my server was up and I made this:

  1. First, install fping

    apt-get install fping
  2. Create an init script in the /etc/init.d folder with the following content (I called it testcon)

    . /lib/lsb/init-functions
    case "$1" in
            log_daemon_msg "Starting internet connection tester"
            /etc/init.d/testcond &
            echo $! > $PIDFILE
            log_end_msg $?
            log_daemon_msg "Stopping internet connection tester"
            PID=$(cat $PIDFILE)
            kill -9 "$PID"
            log_end_msg $?
        echo "Usage: /etc/init.d/testcon {start|stop}"
        exit 1
    exit 0
  3. Create a script in the /etc/init.d folder with the following content (I called it testcond)

    #echo Computer starting or testing daemon init
    while [ "$itest" == "" ] 
        #wait 5 seconds 
        sleep 5
        itest=$(fping | grep alive)
    date | mail -s "Server is up and Internet is online"
    #loop forever
    while [ "1" == "1" ] 
        itest=$(fping | grep alive)
        #if internet is down
        if [ "$itest" == "" ] 
            #echo Internet is down
            #log time it was found down
            echo "Internet was found down @ $current_time" >> /mnt/data/Server/internet_log.txt
            #loop until it is back
            while [ "$itest" == "" ] 
                #wait 60 seconds 
                sleep 60
                itest=$(fping | grep alive) # test the internet
            #when it is back
            echo "Internet is back        @ $current_time" >> /mnt/data/Server/internet_log.txt
            body=$(tail -2 /mnt/data/Server/internet_log.txt)
            echo "$body" | mail -s "Internet is back online"
        #echo Internet is online
        #wait 60 seconds
        sleep 60
  4. Then I run the commands below to add to the start-up:

    sudo update-rc.d testcon defaults
    sudo update-rc.d testcon enable
  5. Reboot

share|improve this answer

Your Answer


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.