How to test an Internet connection with bash?

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

    In Bash, using it's network wrapper through /dev/{udp,tcp}/host/port:

    if : >/dev/tcp/8.8.8.8/53; then
      echo 'Internet available.'
    else
      echo 'Offline.'
    fi
    

    (: is the Bash no-op, because you just want to test the connection, but not processing.)

提交回复
热议问题