# config windows network adapter parameters
# need: python 3.x + wmi + Python for Windows extensions(pywin32)
# wmi: http://pypi.python.org/pypi/WMI/#downloads
# Python for Windows extensions(pywin32): http://sourceforge.net/projects/pywin32/
# code by lymking#hotmail.com
import wmiwmiObj = wmi.WMI()
nicConfigs = wmiObj.Win32_NetworkAdapterConfiguration(IPEnabled = True)
if len(nicConfigs)<1:
print("there not find network adapter in your pc\n byebye...")
exit()
# get & print network adapter name
index=1
for name in nicConfigs:
print(index, name.Caption);
index = index + 1;
cardNo = int(input('please entry a integer number to choose a network adaper: '))
nicConfig = nicConfigs[cardNo-1]
print('current network adapter configuration')
print(nicConfig.IPAddress)
print(nicConfig.IPSubnet)
print(nicConfig.DefaultIPGateway)
print(nicConfig.DNSServerSearchOrder)
# input configuration parameters
ipaddress = [str(input('please input ip address(e.g. 192.168.1.2): '))]
subnetMask = [str(input('please input subnet mask(e.g. 255.255.255.0: )'))]
gateway = [str(input('please input default gateway(e.g. 192.168.1.1): '))]
gatewayCost = [1]
dsnServer = [str(input('please input dsn: '))]
print(ipaddress, subnetMask, gateway, dsnServer)
# return value
reboot = 0
# config IP address & subnetmask
resVal = nicConfig.EnableStatic(IPAddress = ipaddress, SubnetMask = subnetMask)
if resVal[0] == 0:
print("config ip is ok")
elif resVal[0] == 1: # need reboot os
print("config ip is ok")
reboot = reboot + 1
else:
print("config ip is failed!", resVal[0])
exit()
# config default gateway
resVal = nicConfig.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 = nicConfig.SetDNSServerSearchOrder(DNSServerSearchOrder = dsnServer)
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('ip:'.join(nicConfig.IPAddress))
print('subnetmask:'.join(nicConfig.IPSubnet))
print('default gateway:'.join(nicConfig.DefaultIPGateway))
print('DNS:'.join(nicConfig.DNSServerSearchOrder))
print('modify ip is finish!')
# config network adapter is dhcp mode
resVal = nicConfig.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()
评论