How to check internet access using bash script in linux?

前端 未结 10 1191
孤街浪徒
孤街浪徒 2021-02-01 06:43

In my school, the internet is not available(every night after 23:0 the school will kill the internet, to put us in bed >..<), then the ping will never stop, though I have use

相关标签:
10条回答
  • 2021-02-01 07:03

    Reliable old ping in a separate bash script:

    #!/bin/bash
    ipaddr='8.8.8.8' # Google's public DNS server
    [[ -z `ping -c1 $ipaddr |& grep -o 'Network is unreachable'` ]] || exit 1
    [[ -z `ping -c3 $ipaddr |& grep -o '100% packet loss'` ]] && exit 0 || exit 1
    

    Put this on a separate script. It will handle different network situations as (1) not being connected to a network, (2) connected to the network but cannot access the internet (or at least Google), and (3) connected to the internet.


    You may later use the exit code of the script to check connectivity, e.g.

    ~$ script-name && echo online || echo offline
    
    0 讨论(0)
  • 2021-02-01 07:04

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

    0 讨论(0)
  • 2021-02-01 07:07
    #!/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 smth like:

    bash-3.2$ ./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 secs down

    0 讨论(0)
  • 2021-02-01 07:10

    A variation on Majal's solution above is just to test the return code from ping, which returns 0 if the site responds, 1 if there is no reply and 2 if the network is unreachable.

        ping -c 1 -t 5 8.8.8.8 2&>1
        rc=$?
        [[ $rc -eq 0 ]] && { echo "Connected to the Internet" ; exit 0 ; } \
     || [[ $rc -eq 1 ]] && { echo "No reply from Google DNS" ; exit 1 ; } \
     || [[ $rc -eq 2 ]] && { echo "Network unreachable" ; exit 2 ; }
    

    Using ping has the advantage of not needing to download anything, improving the speed of the test.

    0 讨论(0)
  • 2021-02-01 07:12

    Use the timeout option -t:

    ping -q -t 5 -w1 -c1 8.8.8.8 t
    
    0 讨论(0)
  • 2021-02-01 07:22

    Using wget:

    #!/bin/bash
    
    wget -q --tries=10 --timeout=20 --spider http://google.com
    if [[ $? -eq 0 ]]; then
            echo "Online"
    else
            echo "Offline"
    fi
    
    0 讨论(0)
提交回复
热议问题