如何定期检查网络是否正常并重新连接?

如何定期检查网络是否正常并重新连接?

我编写了一个脚本,通过 ping 三个主要站点并断开连接,然后在三个站点都失败时重新连接网络来定期检查我的网络是否正常。即使我没有 X 会话,该脚本也可以在命令行中正常工作。该脚本如下所示:

#!/bin/bash

/usr/bin/notify-send -i 'dialog-information' 'Running CheckNetUp.sh' 'Checking whether network is up'

AtLeastOneSiteFound=false;

# Try to download three sites and indicate a site was found if they work
# as long as we can grab at least one site, we're good.
# Otherwise, we'll disconnect and reconnect.

/usr/bin/wget --spider --read-timeout=15 http://google.ca
if [ "$?" = 0 ]; then
  AtLeastOneSiteFound=true;
fi

/usr/bin/wget --spider --read-timeout=15 http://facebook.com
if [ "$?" = 0 ]; then
  AtLeastOneSiteFound=true;
fi

/usr/bin/wget --spider --read-timeout=15 http://wikipedia.org
if [ "$?" = 0 ]; then
  AtLeastOneSiteFound=true;
fi

if [ "$AtLeastOneSiteFound" = false ]; then
  # Disconnect my wifi (and, consequently, VPN)
  /usr/bin/nmcli dev disconnect wlp0s20u2
  # Bring my wifi up
  /usr/bin/nmcli con up uuid e23f4af0-7411-4f4e-8d3c-a7cd35b607e1
  # Bring my VPN up
  /usr/bin/nmcli con up uuid 6b0f8740-df8e-411e-adeb-bcf70ced772f
fi

问题是这样的:当脚本尝试启动 wifi 时,它会在 cron 中失败,并显示以下错误消息:

Error: Connection activation failed: Not authorized to control networking.

我在网上找到了很多可能的解决方案,但目前还没有成功。最有希望的似乎是让所有用户都可以使用文本配置中的密码连接/访问 wifi 和 VPN 连接系统,但这并没有改变任何东西。

编辑:该脚本在 crontab 中和手动运行时都在我的用户下运行。

答案1

无线上网?

您可以 ping 自己的路由器而不是“主要站点”,连接在您端而不是 ISP 的域服务器或路由器上的某个地方始终保持断开的可能性很小。事实上,脚本方法非常好,以增加的间隔不断 ping LAN 端,看看连接断开需要多长时间?如果您的 LAN 保持运行的时间比您认为不确定的时间长,那么才考虑 ping WAN 端。不过,我建议用 IP 地址替换主机名,以重复 ping 无数的互联网路由器(这应该没问题),而不是任何特定的一台互联网 DNS 服务器(管理员可能不同意)?

您的互联网连接不断下降的事实可能与您的脚本、您的 wget-to 访问受限资源有关?然后是 BIOS 中的电源管理(我真的不确定这如何影响您当前的内核),最后是您的 ISP 对您的问题的响应。再说一次,即使在我醒着的梦中,我也不会考虑使用 Wi-Fi 来向我显示互联网连接问题。

通过铜缆或光纤连接,请重复您的帖子。

相关内容