实验室人多经常出现无故断网,拔网线活着修改一下dns,才能勉强解决。
python编程结束之后,使用python方法打包成exe,然后管理员身份运行即可。
打包exe指令
Pyinstaller -F intnet_binggo.py
具体代码
# -*- coding: utf-8 -*-
"""修改dns代码"""
import os
import random
import re
from time import sleep
from wmi import WMI
#随机修改指定ip段的本机ip
class updateIP:
def __init__(self):
self.wmiService = WMI()
#获取到本地有网卡信息
self.colNicConfigs = self.wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)
print (self.colNicConfigs[0])
def getAdapter(self):
flag = 0
#遍历所有网卡,找到要修改的那个,这里我是用原ip的第一段正则出来的
for obj in self.colNicConfigs:
ip = re.findall("10.\d+.\d+.\d+", obj.IPAddress[0])
if len(ip) > 0:
return flag
else:
flag = flag+1
def runSet(self):
adapter = self.colNicConfigs[self.getAdapter()]
arrDNSServers = ['114.114.114.114'] #dns服务器
x=random.randint(1,2)
if x%2==0:
print("自定义模式")
dnsRes = adapter.SetDNSServerSearchOrder(DNSServerSearchOrder=arrDNSServers)
else:
print("自动获取模式")
dnsRes = adapter.SetDNSServerSearchOrder()
print(dnsRes[0])
if (dnsRes[0] == 0):
print(u'\ttip:设置DNS成功,无须重启')
elif (dnsRes[0] == 1):
print (u'\ttip:设置DNS成功,等待3秒刷新缓存')
sleep(3)
#刷新DNS缓存使DNS生效
os.system('ipconfig /flushdns')
else:
print (u'\ttip:修改DNS失败: DNS设置发生错误')
return False
if __name__ == '__main__':
update = updateIP()
update.runSet()
input()