原文地址:http://shumeipai.nxez.com/2017/01/25/raspberry-pi-wifi-broken-automatically-reconnect.html
实现 WiFi 断线自动重连。原理是用 Python 监测网络是否断线,如果断线则重启网络服务。
1.Python 代码 autowifi.py,放在 /home/pi 目录下:
1
2
3
4
5
6
7
8
|
#!/usr/bin/python
import
os, time
while
True
:
if
'192'
not
in
os.popen(
'ifconfig | grep 192'
).read():
print
'\n****** wifi is down, restart... ******\n'
os.system(
'sudo /etc/init.d/networking restart'
)
time.sleep(
5
*
60
)
#5 minutes
|
2.Shell脚本autowifi.sh,也放在 /home/pi 目录下:
1
2
|
#!/bin/sh
python
/home/pi/autowifi
.py &
|
3.开机自动启动以上脚本:在终端窗口执行以下命令即可
1
2
3
4
|
sudo
cp
-f
/home/pi/autowifi
.sh
/etc/init
.d/
sudo
chmod
+x
/etc/init
.d
/autowifi
.sh
sudo
chown
root:root
/etc/init
.d
/autowifi
.sh
sudo
update-rc.d autowifi.sh default
|