检查是否联网,没有联网就自动重连wifi Guest。
bash展开代码mkdir/root/autowifi/ && vim /root/autowifi/wifi-watchdog.sh
写入:
bash展开代码#!/bin/bash
interface="wlan0" # 请根据你的实际接口名修改,如 wlp3s0
while true; do
if ! ping -c 1 www.baidu.com &> /dev/null; then
echo "WiFi disconnected. Attempting to reconnect..."
sudo systemctl restart NetworkManager
sleep 5
sudo nmcli device wifi connect "Guest" password "Guest"
fi
sleep 300 # 每300秒检查一次网络状态
done
使用systemd服务
bash展开代码sudo vim /etc/systemd/system/wifi-watchdog.service
写入:
bash展开代码[Unit] Description=WiFi Auto-Reconnect Service After=network.target [Service] Type=simple ExecStart=/bin/bash /root/autowifi/wifi-watchdog.sh Restart=always User=root [Install] WantedBy=multi-user.target
启用并启动服务:
bash展开代码sudo systemctl daemon-reload
sudo systemctl enable wifi-watchdog.service
sudo systemctl start wifi-watchdog.service
验证服务状态:
bash展开代码sudo systemctl status wifi-watchdog.service
如果看到 active (running)表示成功。
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!