python实现自动更换ip的方法

时间:2021-05-22

本文实例讲述了python实现自动更换ip的方法。分享给大家供大家参考。具体实现方法如下:

#!/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程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章