#!/usr/bin/env python#-*- encoding:gb2312 -*-# filename: ip.pyimport sitecustomizeimport _winregimport configparserfrom ctypes import *print '正在进行网络适配器检测,请稍候…'printnetcfginstanceid = nonehkey = _winreg.openkey(_winreg.hkey_local_machine, \r'system\currentcontrolset\control\class\{4d36e972-e325-11ce-bfc1-08002be10318}')keyinfo = _winreg.queryinfokey(hkey)# 寻找网卡对应的适配器名称 netcfginstanceidfor index in range(keyinfo[0]):hsubkeyname = _winreg.enumkey(hkey, index)hsubkey = _winreg.openkey(hkey, hsubkeyname)try:hndiinfkey = _winreg.openkey(hsubkey, r'ndi\interfaces')lowerrange = _winreg.queryvalueex(hndiinfkey, 'lowerrange')# 检查是否是以太网if lowerrange[0] == 'ethernet':driverdesc = _winreg.queryvalueex(hsubkey, 'driverdesc')[0]print '检测到网络适配器名:', driverdescnetcfginstanceid = _winreg.queryvalueex(hsubkey, 'netcfginstanceid')[0]print '检测到网络适配器id:', netcfginstanceidif netcfginstanceid == none:print '没有找到网络适配器,程序退出'exit()break_winreg.closekey(hndiinfkey)except windowserror:print r'message: no ndi\interfaces key'# 循环结束,目前只提供修改一个网卡ip的功能_winreg.closekey(hsubkey)_winreg.closekey(hkey)# 通过修改注册表设置ipstrkeyname = 'system\\currentcontrolset\\services\\tcpip\\parameters\\interfaces\\' + netcfginstanceidprint '网络适配器的注册表地址是:\n', strkeynamehkey = _winreg.openkey(_winreg.hkey_local_machine, \strkeyname, \0, \_winreg.key_write)config = configparser.configparser()printprint '正在打开ip.ini配置文件…'config.readfp(open('ip.ini'))ipaddress = config.get(school,ipaddress)subnetmask = config.get(school,subnetmask)gateway = config.get(school,gateway)dnsserver1 = config.get(school,dnsserver1)dnsserver2 = config.get(school,dnsserver2)dnsserver = [dnsserver1,dnsserver2]print '配置文件内设定的信息如下,请核对:'printprint 'ip地址:', ipaddressprint '子关掩码:', subnetmaskprint '默认网关:', gatewayprint '主dns服务器:', dnsserver1print '次dns服务器:', dnsserver2printres = raw_input('现在,请您决定:输入1,则将配置文件写入系统;输入2,则将现有的系统设定还原为全部自动获取;否则程序退出:')if str(res) == '1':try:_winreg.setvalueex(hkey, 'enabledhcp', none, _winreg.reg_dword, 0x00000000)_winreg.setvalueex(hkey, 'ipaddress', none, _winreg.reg_multi_sz, [ipaddress])_winreg.setvalueex(hkey, 'subnetmask', none, _winreg.reg_multi_sz, [subnetmask])_winreg.setvalueex(hkey, 'defaultgateway', none, _winreg.reg_multi_sz, [gateway])_winreg.setvalueex(hkey, 'nameserver', none, _winreg.reg_sz, ','.join(dnsserver))except windowserror:print 'set ip error'exit()_winreg.closekey(hkey)print '切换成功!重置网络后即可生效'elif str(res) == '2':try:_winreg.setvalueex(hkey, 'enabledhcp', none, _winreg.reg_dword, 0x00000001)_winreg.setvalueex(hkey, 't1', none, _winreg.reg_dword, 0x00000000)_winreg.setvalueex(hkey, 't2', none, _winreg.reg_dword, 0x00000000)_winreg.setvalueex(hkey, 'nameserver', none, _winreg.reg_sz, none)_winreg.setvalueex(hkey, 'dhcpconnforcebroadcastflag', none, _winreg.reg_dword, 0x00000000)_winreg.setvalueex(hkey, 'lease', none, _winreg.reg_dword, 0x00000000)_winreg.setvalueex(hkey, 'leaseobtainedtime', none, _winreg.reg_dword, 0x00000000)_winreg.setvalueex(hkey, 'leaseterminatestime', none, _winreg.reg_dword, 0x00000000)except windowserror:print 'set ip error'exit()_winreg.closekey(hkey)print '切换成功!重置网络后即可生效'else:print '用户手动取消,程序退出'exit('')
希望本文所述对大家的python程序设计有所帮助。
