如何通过命令行判断wifi是否断开?

如何通过命令行判断wifi是否断开?

Ubuntu 版本:20.04
Shell:bash
内核版本:5.8.0-53-generic #60~20.04.1-Ubuntu

我有一台 24/7 运行加密货币工作负载的 PC,它必须连接到 wifi。我注意到,每天至少有一次,wifi 断线,并且不会自动重新连接。wifi 图标会熄灭,并显示一个问号,如下所示。

带问号标志的 Wifi 图标

我必须手动运行网络管理器重启(如下所示)来修复该问题。

sudo service network-manager restart

在我找到永久解决方案之前,我正在考虑每 5 分钟运行一次脚本,检查 wifi 是否已关闭,如果已关闭,它将运行网络管理器重新启动。如何从命令行确定 wifi 是否已关闭?

答案1

如果找不到其他解决方案,您可以尝试类似的方法:

tail -f /var/log/syslog | grep --line-buffered ' wlo1: CTRL-EVENT-DISCONNECTED ' \
| while read line
do
  echo "Caught: $line"
  echo "Restarting network-manager..."
  sudo service network-manager restart
done

您可以将字符串修改grep为与您的系统匹配的内容。

答案2

命令行工具nmcli使用以下命令进行连通性检查:

    nmcli networking connectivity

来自手册页

   connectivity [check]
       Get network connectivity state. The optional check argument tells NetworkManager to
       re-check the connectivity, else the most recent known connectivity state is displayed
       without re-checking.

       Possible states are:

       none
           the host is not connected to any network.

       portal
           the host is behind a captive portal and cannot reach the full Internet.

       limited
           the host is connected to a network, but it has no access to the Internet.

       full
           the host is connected to a network and has full access to the Internet.

       unknown
           the connectivity status cannot be found out.

当您运行nmcli networking connectivity命令时,它会返回以下任一值:

无、门户、有限、完整、未知

您可以根据所需的值准备脚本。

相关内容