PyCharm开发Python利用WMI修改电脑IP、DNS

1.在PyCharm开发中安装WMI插件
PyCharm开发Python利用WMI修改电脑IP、DNS_第1张图片
PyCharm开发Python利用WMI修改电脑IP、DNS_第2张图片
搜索需要添加的WM插件,并安装,安装成功后会有提示!
PyCharm开发Python利用WMI修改电脑IP、DNS_第3张图片

PyCharm开发Python利用WMI修改电脑IP、DNS_第4张图片

2.源代码:

"""=== coding: UTF8 ==="""

import wmi

# IP地址
arrIPAddresses = ['192.168.30.79']
# 子网掩码
arrSubnetMasks = ['255.255.255.0']
# 默认网关
arrDefaultGateways = ['192.168.30.1']
arrGatewayCostMetrics = [1]
# DNS列表
arrDNS = ['114.114.114.114', '114.114.115.115']

'''================Internet操作类================'''
class InternetDef(object):
    intReboot = 0

    """初始化函数"""
    def __init__(self, arrIPAddressesServer, arrSubnetMasksServer,
                 arrDefaultGatewaysServer, arrGatewayCostMetricsServer, arrDNSServers,
                 wmiService, colNicConfigs, objNicConfig):
        self.arrIPAddressesServer = arrIPAddressesServer
        self.arrSubnetMasksServer = arrSubnetMasksServer
        self.arrDefaultGatewaysServer = arrDefaultGatewaysServer
        self.arrGatewayCostMetricsServer = arrGatewayCostMetricsServer
        self.arrDNSServers = arrDNSServers
        self.wmiService = wmiService
        self.colNicConfigs = colNicConfigs
        self.objNicConfig = objNicConfig

    """启动WMI服务函数"""
    def StartWmiService(self):
        # 创建WMI服务对象
        self.wmiService = wmi.WMI()
        # 调用网络适配器配置
        self.colNicConfigs = self.wmiService.Win32_NetworkAdapterConfiguration(IPEnabled=True)

        if len(self.colNicConfigs) < 1:
            print("没有找到可用的网络适配器!")
            return False
        else:
            self.objNicConfig = self.colNicConfigs[0]
            return True

    """设置IP服务函数"""
    def SetIPServers(self):
        returnValue = self.objNicConfig.EnableStatic(IPAddress=self.arrIPAddressesServer,
                                                     SubnetMask=self.arrSubnetMasksServer)
        if returnValue[0] == 0:
            print("修改IP成功")
        elif returnValue[0] == 1:
            print("修改IP成功")
            InternetDef.intReboot += 1
        else:
            print("修改IP失败")
            exit()

    """设置默认网关服务函数"""
    def SetGatewaysServer(self):
        returnValue = self.objNicConfig.SetGateways(DefaultIPGateway=self.arrDefaultGatewaysServer,
                                                    GatewayCostMetric=self.arrGatewayCostMetricsServer)
        if returnValue[0] == 0:
            print("修改网关成功")
        elif returnValue[0] == 1:
            print("修改网关成功")
            InternetDef.intReboot += 1
        else:
            print("修改网关失败")
            exit()

    """设置DNS服务函数"""
    def SetDNSServers(self):
        returnValue = self.objNicConfig.SetDNSServerSearchOrder(DNSServerSearchOrder=self.arrDNSServers)

        if returnValue[0] == 0:
            print("修改DNS成功")
        elif returnValue[0] == 1:
            print("修改DNS成功")
            InternetDef.intReboot += 1
        else:
            print("修改DNS失败")
            exit()

    """设置Internet服务函数"""
    def SetInternetServer(self):
        if self.StartWmiService():  # 启动WMI服务,如果返回True,则设置Internet
            self.SetIPServers()
            self.SetGatewaysServer()
            self.SetDNSServers()
        else:
            exit()

    """获取IP信息函数"""
    def GetIPInfo(self):
        print("设置的IP:\n", self.objNicConfig.IPAddress[0])
        print("设置的子网掩码:\n", self.objNicConfig.IPSubnet[0])

    """获取网关信息函数"""
    def GetGatewaysInfo(self):
        print("设置的网关:\n", self.objNicConfig.DefaultIPGateway[0])

    """获取DNS信息函数"""
    def GetDNSInfo(self):
        print("设置的DNS:\n" + '\n'.join(self.objNicConfig.DNSServerSearchOrder))

    """获取Internet所有信息函数"""
    def GetInternetInfo(self):
        if InternetDef.intReboot > 0:
            print("需要重新启动计算机!")

        if self.StartWmiService():  # 重新启动WMI服务,如果返回True,则获取internet信息
            self.GetIPInfo()
            self.GetGatewaysInfo()
            self.GetDNSInfo()
        else:
            exit()


"""================主程序================"""""
# 定义internet对象
internet1 = InternetDef(arrIPAddresses, arrSubnetMasks, arrDefaultGateways,
                        arrGatewayCostMetrics, arrDNS, None, None, None)

#设置Internet
internet1.SetInternetServer()

#获取internet信息
internet1.GetInternetInfo()

# Python程序运行结束后不关闭控制台命令
input("\n请按任意键退出。。。")

3.编译输出

E:\PythonSourceCode\SetDNS\venv\Scripts\python.exe E:/PythonSourceCode/SetDNS/SetDNS.py
修改IP成功
修改网关成功
修改DNS成功
设置的IP:
 192.168.30.79
设置的子网掩码:
 255.255.255.0
设置的网关:
 192.168.30.1
设置的DNS:
114.114.114.114
114.114.115.115

请按任意键退出。。。

你可能感兴趣的:(Python+PyCharm,Python,WMI修改DNS,PyCharm安装WMI插件,Python,WMI修改IP)