How can I check Internet access using a Bash script on Linux?

73,885

Solution 1

Using wget:

#!/bin/bash

wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
        echo "Online"
else
        echo "Offline"
fi

Solution 2

If the school actually turns off their router instead of redirecting all traffic to a "why aren't you in bed" page, then there's no need to download an entire web page or send HTTP headers. All you have to do is just make a connection and check if someone's listening.

nc -z 8.8.8.8 53

This will output "Connection to 8.8.8.8 port 53 [tcp/domain] succeeded!" and return a value of 0 if someone's listening.

If you want to use it in a shell script:

nc -z 8.8.8.8 53  >/dev/null 2>&1
online=$?
if [ $online -eq 0 ]; then
    echo "Online"
else
    echo "Offline"
fi

Solution 3

Use:

#!/bin/bash

INTERNET_STATUS="UNKNOWN"
TIMESTAMP=`date +%s`
while [ 1 ]
 do
    ping -c 1 -W 0.7 8.8.4.4 > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        if [ "$INTERNET_STATUS" != "UP" ]; then
            echo "UP   `date +%Y-%m-%dT%H:%M:%S%Z` $((`date +%s`-$TIMESTAMP))";
            INTERNET_STATUS="UP"
        fi
    else
        if [ "$INTERNET_STATUS" = "UP" ]; then
            echo "DOWN `date +%Y-%m-%dT%H:%M:%S%Z` $((`date +%s`-$TIMESTAMP))";
            INTERNET_STATUS="DOWN"
        fi
    fi
    sleep 1
 done;

The output will produce something like:

./internet_check.sh

UP   2016-05-10T23:23:06BST 4
DOWN 2016-05-10T23:23:25BST 19
UP   2016-05-10T23:23:32BST 7

The number in the end of a line shows duration of previous state, i.e. 19 up, 7 seconds down.

Solution 4

Install fping: > fewer problems than ping.

fping google.com | grep alive

To use, for example, like:

#!/bin/bash

itest=$(fping google.com | grep alive)

while [ "$itest" == "" ]
        do
        sleep 5
        itest=$(fping google.com | grep alive)
done
echo now online

Solution 5

Without wget

#!/bin/bash

echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "Online"
else
    echo "Offline"
fi

Enjoy ;)

Share:
73,885

Related videos on Youtube

Libin Wen
Author by

Libin Wen

I am an electron, little but free.

Updated on July 09, 2022

Comments

  • Libin Wen
    Libin Wen almost 2 years

    In my school, the Internet is not available (every night after 23:00 the school will kill the Internet connection, to put us to bed >..<), Then the ping will never stop, though I have used the parameter ping -w1 ....

    That is, when I use: ping -q -w1 -c1 8.8.8.8 to check if the Internet connection is up/down, it will be there without any output and doesn't exit, just like I am using a single cat.

    I don't know why it's like this, But I think the problem is related to the school-internet-service. Any suggestion? (I think wget may be a good alternative, but how can I use it?)

  • umläute
    umläute about 11 years
    one problem with this solution is, that some networks might block outgoing pings (my uni had a famous record for doing so). it doesn't seem the be the case with the OP though.
  • Eddy_Em
    Eddy_Em about 11 years
    @umlaeute, in that case you may use wget http://google.com or even (better & simpler) curl
  • Libin Wen
    Libin Wen about 11 years
    It works well. Thank you very much! My old version lacks a --tries, thus it doesn't work.
  • Andrew Ferrier
    Andrew Ferrier over 10 years
    Personally, I enhance this pattern by making this wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null. That throws away the output, which means that files aren't left lying around, and there aren't any problems with write permissions.
  • peterh
    peterh almost 10 years
    You really should use --spider option as it will send a http HEAD request as opposed to a http GET request. Now, in this case you're checking against google.com which is a pretty light weight page so it may be ok as it is. But as a general rule you should use a HEAD request if you just want to check if something is available without actually downloading it. I've added to the answer accordingly.
  • Yoni
    Yoni over 8 years
    This is the fastest approach, it pings the dns server instead of getting google's website data. thumbs up.
  • Benjamin W.
    Benjamin W. about 8 years
    Welcome to Stack Overflow! While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run.
  • AShah
    AShah over 5 years
    might be good to use a timeout here nc -z -w 5 8.8.8.8 53 >/dev/null 2>&1
  • rhand
    rhand over 5 years
    Had lots of ups and downs in a few minutes. While DropBox was online and surfing the web was possible... See unix.stackexchange.com/a/190610/19694 where they mention nc would be better to use than ping as quite a few hosters disable ICMP as well.
  • John Wooten
    John Wooten over 4 years
    I fail to see how I would get the duration with the code above. I tried it and the 'duration' for each line grew monotonically. Doesn't TIMESTAMP have to be reset each time through the loop?
  • Scott Fleming
    Scott Fleming over 4 years
    fping would need to be installed before running the example.