前言:由于电脑被加入域控,每隔1-2小时无线网络会被禁用不能连接外网,手工重启费时费力,故写Python脚本以代替手工重启过程。以此类推,此脚本更换服务名称也可以用于启动其它Windows服务。
一、手工重启过程
(1)WIN+R输入services.msc
![](https://img.haomeiwen.com/i6128621/cfe9b9c9f262f0f9.png)
(2)找到相应服务,双击
![](https://img.haomeiwen.com/i6128621/197d2a44a4dcd4ef.png)
(3)更改启动类型与服务状态
![](https://img.haomeiwen.com/i6128621/be835c117ff033af.png)
二、Python执行脚本
(1)思路:
执行net start命令,在已开服务列表中寻找WLAN AutoConfig是否开启,如果未开启则启动相应服务,每10S执行一次循环,此脚本需要以管理员身份运行CMD执行。
(2)脚本:
import os
import time
command_1="net start"
command_2="net start Wlansvc"
command_3="sc config Wlansvc start= auto"
flag=True
count=0
while True:
flag=True
r=os.popen(command_1)
for line in r.readlines():
line=line.strip('\r\n')
line=line.strip()
if line[0:15]=="WLAN AutoConfig":
flag=True
break
flag=False
if flag==False:
count=count+1
print(count)
os.popen(command_3)
os.popen(command_2)
time.sleep(10)
网友评论