How to test an Internet connection with bash?

后端 未结 19 1439
青春惊慌失措
青春惊慌失措 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: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

提交回复
热议问题