加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
修改IP地址网关和DNS.py 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2022-06-09 13:24 . 添加修改IP地址
# -*- 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'])
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化