#!/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()
# -*- 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()
本博客介绍如何使用Python脚本修改计算机网络适配器的配置参数,包括IP地址、子网掩码、默认网关和DNS服务器设置。
2457

被折叠的 条评论
为什么被折叠?



