原理很简单,通过python执行dos命令 : ping 和 netsh
需要用到os和time模块
代码如下:
>>> import os >>> print 'wifi掉线' wifi掉线 >>> import time >>> while True: ... status = os.system("ping -c 1 www.baidu.com") ... if status == 1: ... print 'wifi掉线' ... os.system('netsh wlan connect name=DSGY2') ... else: ... print '连接状态' ... time.sleep(180)
大家使用时将name=XX ,名字改一下,在我的机子上测试通过,该脚本3分钟检测一次是否掉线
如果你的wifi在手动连接都连不上,那这段脚本就没用了
------------------------------------------------------------------------------------------------------------------------
Pinging www.a.shifen.com [14.215.177.37] with 32 bytes of data:
Reply from 14.215.177.37: bytes=32 time=24ms TTL=54
Reply from 14.215.177.37: bytes=32 time=63ms TTL=54
Reply from 14.215.177.37: bytes=32 time=32ms TTL=54
Reply from 14.215.177.37: bytes=32 time=26ms TTL=54
Ping statistics for 14.215.177.37:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 24ms, Maximum = 63ms, Average = 36ms
连接状态
Pinging www.a.shifen.com [180.97.33.108] with 32 bytes of data:
Reply from 180.97.33.108: bytes=32 time=1676ms TTL=54
Reply from 180.97.33.108: bytes=32 time=544ms TTL=54
Reply from 180.97.33.108: bytes=32 time=2033ms TTL=54
Reply from 180.97.33.108: bytes=32 time=291ms TTL=54
Ping statistics for 180.97.33.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 291ms, Maximum = 2033ms, Average = 1136ms
连接状态
Pinging www.a.shifen.com [180.97.33.107] with 32 bytes of data:
Reply from 180.97.33.107: bytes=32 time=126ms TTL=54
Reply from 180.97.33.107: bytes=32 time=294ms TTL=54
Reply from 180.97.33.107: bytes=32 time=57ms TTL=54
Reply from 180.97.33.107: bytes=32 time=131ms TTL=54
Ping statistics for 180.97.33.107:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 57ms, Maximum = 294ms, Average = 152ms
连接状态
----------------------------------------------------------------------------------------------------------------
昨天没有把代码保存,也没有碰到掉线的情况
#-*-coding:utf-8-*- import os import time while True: status = os.system('ping -c 1 www.baidu.com') if status == 1: print u'wifi掉线' os.system('netsh wlan connect name=haha') else: print u'连接状态' time.sleep(30)
运行情况
Pinging www.a.shifen.com [180.97.33.108] with 32 bytes of data:
Reply from 180.97.33.108: bytes=32 time=21ms TTL=56
Reply from 180.97.33.108: bytes=32 time=22ms TTL=56
Reply from 180.97.33.108: bytes=32 time=45ms TTL=56
Reply from 180.97.33.108: bytes=32 time=25ms TTL=56
Ping statistics for 180.97.33.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 21ms, Maximum = 45ms, Average = 28ms
连接状态
Ping request could not find host www.baidu.com. Please check the name and try again.
wifi掉线
Connection request was completed successfully.
Pinging www.a.shifen.com [180.97.33.108] with 32 bytes of data:
Reply from 180.97.33.108: bytes=32 time=26ms TTL=56
Reply from 180.97.33.108: bytes=32 time=27ms TTL=56
Reply from 180.97.33.108: bytes=32 time=27ms TTL=56
Reply from 180.97.33.108: bytes=32 time=28ms TTL=56
Ping statistics for 180.97.33.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 26ms, Maximum = 28ms, Average = 27ms
连接状态
---注意:在管理员权限下运行脚本