使用 Bash 中的 Ping 命令评估互联网连接是否正常

问题描述 投票:0回答:1
  1. 我想使用 bash 评估互联网连接是否正常
  2. 因此,我正在 ping 随机网站来测试连接
  3. 如果已启动,则执行操作 1。
  4. 如果已关闭,请执行操作 2。

我写了以下内容

    if ping -q -w 1 -c 1 https://www.example.com > /dev/null
    then
            echo "Internet is up"

    else
            echo "Internet is down"

此外,如果您能建议我一种使用curl/grep 执行相同操作的方法,那就太好了

bash shell if-statement curl ping
1个回答
0
投票

使用

nc
实用程序,类似

    # After hostname, the port can be specified
    # Let's set connection timeout to 1 sec
    if nc -zw1 $hostname >/dev/null 2>&1
    then
        echo "'$hostname' is reachable!"
    fi
© www.soinside.com 2019 - 2024. All rights reserved.