正文

[代码]python配置网络参数(IP\子网掩码\网关\DNS\DHCP模式)2011-11-08 14:56:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lym51/52912.html

分享到:

# 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 wmi

wmiObj = 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()
       

阅读(4203) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册