modify network adapter configuration parameters

本博客介绍如何使用Python脚本修改计算机网络适配器的配置参数,包括IP地址、子网掩码、默认网关和DNS服务器设置。
#!/usr/bin/env python
# -*- coding: cp936 -*-




# Author: Jack
# Purpose: modify network adapter configuration parameters
# date: March 30,2012


import wmi




class ModifyNetworkAdapterConfiguration:
    def __init__(self, wmi_object):
        self._nnetwork_adapters = wmi_object.Win32_NetworkAdapterConfiguration(IPEnabled = True)
        if len(self._nnetwork_adapters)<1:
            print("there not find network adapter in your pc\n byebye...")
            exit()
        
    def print_configuration_one(self):
        for obj_network_adapter in self._nnetwork_adapters:
            print obj_network_adapter.Index
            print obj_network_adapter.SettingID
            print obj_network_adapter.Description.encode("cp936")
            print obj_network_adapter.IPAddress
            print obj_network_adapter.IPSubnet
            print obj_network_adapter.DefaultIPGateway
            print obj_network_adapter.DNSServerSearchOrder
    
    def print_configuration_two(self):
        index=1
        for obj_network_adapter in self._nnetwork_adapters:
            print(index, obj_network_adapter.Caption)
            index = index + 1;
            
    def run_one(self):
        self.print_configuration_two()
        
        index = input('please enter order number for network adapter configuration to want view: ')
        nnetwork_adapter = self._nnetwork_adapters[index-1]
        
        print('current network adapter configuration parameters: ')
        print('IP Address: ', nnetwork_adapter.IPAddress)
        print('Subnet Mask: ', nnetwork_adapter.IPSubnet)
        print('Gateway: ', nnetwork_adapter.DefaultIPGateway)
        print('DNS: ', nnetwork_adapter.DNSServerSearchOrder)
        
        ip_address = [raw_input('please input a new ip address for change it: ')]
        subnet_mask = [raw_input('please input a new subnet mask for change it: ')]
        gateway = [raw_input('please input a new gateway for change it: ')]
        gatewayCost = [1]
        dns = [raw_input('please input a new dns for change it: ')]
        
        reboot = 0
        
        # config IP address & subnetmask
        resVal = nnetwork_adapter.EnableStatic(IPAddress = ip_address, SubnetMask = subnet_mask)
        if resVal[0] == 0:
            print("config ip and subnet mask is ok")
        elif resVal[0] == 1: # need reboot os
            print("config ip and subnet mask is ok")
            reboot = reboot + 1
        else:
            print("config ip and subnet mask is failed!", resVal[0])
            exit()
            
        # config default gateway   
        resVal = nnetwork_adapter.SetGateways(DefaultIPGateway = gateway, GatewayCostMetric = gatewayCost)
        if resVal[0] == 0:
            print("config gateway is ok")
        elif resVal[0] == 1: # need reboot os
            print("config gateway is ok")
            reboot = reboot + 1
        else:
            print("config gateway is failed!")
            exit()
            
        # config dns   
        resVal = nnetwork_adapter.SetDNSServerSearchOrder(DNSServerSearchOrder = dns)
        if resVal[0] == 0:
            print("config dns is ok")
        elif resVal[0] == 1: # need reboot os
            print("config dns is ok")
            reboot = reboot + 1
        else:
            print("config dns is failed!")
            exit()
            
        if reboot > 0:
            print('please reboot your os')
        else:
            print('modify network adapter configuration parameters: ')
            print('ip:'.join(nnetwork_adapter.IPAddress))
            print('subnetmask:'.join(nnetwork_adapter.IPSubnet))
            print('default gateway:'.join(nnetwork_adapter.DefaultIPGateway))
            print('DNS:'.join(nnetwork_adapter.DNSServerSearchOrder))
           
        print('modify ip is finish!')
        
    def run_two(self):
        self.print_configuration_two()
        
        index = input('please enter order number for network adapter configuration to want view: ')
        nnetwork_adapter = self._nnetwork_adapters[index-1]
        
        reboot = 0
        
        # config network adapter is dhcp mode
        resVal = nnetwork_adapter.EnableDHCP()
        if resVal[0] == 0:
            print('dhcp mode is ok')
        elif resVal[0] == 1:
            print('dhcp mode is ok')
            reboot = reboot + 1
        else:
            print('dhcp mode is failed')
            exit()
            
        ## config network adapter is dns mode
        #resVal = nnetwork_adapter.EnableDNS(, , , , )
        #if resVal[0] == 0:
        #    print('dns mode is ok')
        #elif resVal[0] == 1:
        #    print('dns mode is ok')
        #    reboot = reboot + 1
        #else:
        #    print('dns mode is failed')
        #    exit()
            
        if reboot > 0:
            print('please reboot your os')
        else:
            print('modify network adapter configuration parameters: ')
            print('ip:'.join(nnetwork_adapter.IPAddress))
            print('subnetmask:'.join(nnetwork_adapter.IPSubnet))
            print('default gateway:'.join(nnetwork_adapter.DefaultIPGateway))
            print('DNS:'.join(nnetwork_adapter.DNSServerSearchOrder))
           
        print('modify ip is finish!')
        
if __name__ == '__main__':
    wmiObj = wmi.WMI()
    modify = ModifyNetworkAdapterConfiguration(wmiObj)
    modify.run_one()
        
        
    
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值