How to test an Internet connection with bash?

后端 未结 19 1208
青春惊慌失措
青春惊慌失措 2020-11-27 09:26

How can an internet connection be tested without pinging some website? I mean, what if there is a connection but the site is down? Is there a check for a connection with the

相关标签:
19条回答
  • 2020-11-27 10:03

    Execute the following command to check whether a web site is up, and what status message the web server is showing:

    $ curl -Is http://www.google.com | head -1 HTTP/1.1 200 OK

    Status code ‘200 OK’ means that the request has succeeded and a website is reachable.

    0 讨论(0)
  • 2020-11-27 10:04

    This works on both MacOSX and Linux:

    #!/bin/bash
    
    ping -q -w1 -c1 google.com &>/dev/null && echo online || echo offline
    
    0 讨论(0)
  • 2020-11-27 10:05

    The top voted answer does not work for MacOS so for those on a mac, I've successfully tested this:

    GATEWAY=`route -n get default | grep gateway`
    if [ -z "$GATEWAY" ]
      then
        echo error
    else
      ping -q -t 1 -c 1 `echo $GATEWAY | cut -d ':' -f 2` > /dev/null && echo ok || echo error
    fi
    

    tested on MacOS High Sierra 10.12.6

    0 讨论(0)
  • 2020-11-27 10:06

    Super Thanks to user somedrew for their post here: https://bbs.archlinux.org/viewtopic.php?id=55485 on 2008-09-20 02:09:48

    Looking in /sys/class/net should be one way

    Here's my script to test for a network connection other than the loop back. I use the below in another script that I have for periodically testing if my website is accessible. If it's NOT accessible a popup window alerts me to a problem.

    The script below prevents me from receiving popup messages every five minutes whenever my laptop is not connected to the network.

    #!/usr/bin/bash
    
    # Test for network conection
    for interface in $(ls /sys/class/net/ | grep -v lo);
    do
      if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then OnLine=1; fi
    done
    if ! [ $OnLine ]; then echo "Not Online" > /dev/stderr; exit; fi
    

    Note for those new to bash: The final 'if' statement tests if NOT [!] online and exits if this is the case. See man bash and search for "Expressions may be combined" for more details.

    P.S. I feel ping is not the best thing to use here because it aims to test a connection to a particular host NOT test if there is a connection to a network of any sort.

    P.P.S. The Above works on Ubuntu 12.04 The /sys may not exist on some other distros. See below:

    Modern Linux distributions include a /sys directory as a virtual filesystem (sysfs, comparable to /proc, which is a procfs), which stores and allows modification of the devices connected to the system, whereas many traditional UNIX and Unix-like operating systems use /sys as a symbolic link to the kernel source tree.[citation needed]

    From Wikipedia https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard

    0 讨论(0)
  • 2020-11-27 10:08

    Checking Google's index page is another way to do it:

    #!/bin/bash
    
    WGET="/usr/bin/wget"
    
    $WGET -q --tries=20 --timeout=10 http://www.google.com -O /tmp/google.idx &> /dev/null
    if [ ! -s /tmp/google.idx ]
    then
        echo "Not Connected..!"
    else
        echo "Connected..!"
    fi
    
    0 讨论(0)
  • 2020-11-27 10:09

    If your local nameserver is down,

    ping 4.2.2.1
    

    is an easy-to-remember always-up IP (it's actually a nameserver, even).

    0 讨论(0)
提交回复
热议问题