代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 9 13:22:47 2022
@author: fengmingshan
"""
import os
import re
from time import sleep
from wmi import WMI
class UpdateIp(object):
def __init__(self):
self.wmiservice = WMI()
self.configs = self.wmiservice.Win32_NetworkAdapterConfiguration(IPEnabled=True) # 获取到本地所有有网卡信息,list
def get_inter(self):
flag = 0
# 遍历所有网卡,找到要修改的那个
for con in self.configs:
ip = re.findall("\d+.\d+.\d+.\d+", con.IPAddress[0])
if len(ip) > 0:
return 0
else:
flag = flag+1
def runset(self, ip, subnetmask, interway, dns):
adapter = self.configs[self.get_inter()]
# 开始执行修改ip、子网掩码、网关
ipres = adapter.EnableStatic(IPAddress=ip, SubnetMask=subnetmask)
if ipres[0] == 0:
print('设置IP成功')
else:
if ipres[0] == 1:
print('设置IP成功,需要重启计算机!')
else:
print('修改IP失败')
return False
#修改网关
wayres = adapter.SetGateways(DefaultIPGateway=interway, GatewayCostMetric=[1])
if wayres[0] == 0:
print('设置网关成功')
else:
print('修改网关失败')
return False
#修改dns
dnsres = adapter.SetDNSServerSearchOrder(DNSServerSearchOrder=dns)
if dnsres[0] == 0:
print('设置DNS成功,等待3秒刷新缓存')
sleep(3)
# 刷新DNS缓存使DNS生效
os.system('ipconfig /flushdns')
else:
print('修改DNS失败')
return False
if __name__ == '__main__':
update = UpdateIp()
update.runset(['135.40.13.144'], ['255.255.255.192'], ['135.40.13.129'], ['10.104.1.23'])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。