python實現(xiàn)自動更換ip的方法
更新時間:2015年05月05日 16:14:42 作者:songguo
這篇文章主要介紹了python實現(xiàn)自動更換ip的方法,涉及Python針對本機網(wǎng)絡(luò)配置的相關(guān)操作技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了python實現(xiàn)自動更換ip的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
#!/usr/bin/env python #-*- encoding:gb2312 -*- # Filename: IP.py import sitecustomize import _winreg import ConfigParser from ctypes import * print '正在進行網(wǎng)絡(luò)適配器檢測,請稍候…' print netCfgInstanceID = None hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, \ r'System\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}') keyInfo = _winreg.QueryInfoKey(hkey) # 尋找網(wǎng)卡對應(yīng)的適配器名稱 netCfgInstanceID for 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') # 檢查是否是以太網(wǎng) if lowerRange[0] == 'ethernet': driverDesc = _winreg.QueryValueEx(hSubKey, 'DriverDesc')[0] print '檢測到網(wǎng)絡(luò)適配器名:', driverDesc netCfgInstanceID = _winreg.QueryValueEx(hSubKey, 'NetCfgInstanceID')[0] print '檢測到網(wǎng)絡(luò)適配器ID:', netCfgInstanceID if netCfgInstanceID == None: print '沒有找到網(wǎng)絡(luò)適配器,程序退出' exit() break _winreg.CloseKey(hNdiInfKey) except WindowsError: print r'Message: No Ndi\Interfaces Key' # 循環(huán)結(jié)束,目前只提供修改一個網(wǎng)卡IP的功能 _winreg.CloseKey(hSubKey) _winreg.CloseKey(hkey) # 通過修改注冊表設(shè)置IP strKeyName = 'System\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\' + netCfgInstanceID print '網(wǎng)絡(luò)適配器的注冊表地址是:\n', strKeyName hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, \ strKeyName, \ 0, \ _winreg.KEY_WRITE) config = ConfigParser.ConfigParser() print print '正在打開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 '配置文件內(nèi)設(shè)定的信息如下,請核對:' print print 'IP地址:', IPAddress print '子關(guān)掩碼:', SubnetMask print '默認(rèn)網(wǎng)關(guān):', GateWay print '主DNS服務(wù)器:', DNSServer1 print '次DNS服務(wù)器:', DNSServer2 print res = raw_input('現(xiàn)在,請您決定:輸入1,則將配置文件寫入系統(tǒng);輸入2,則將現(xiàn)有的系統(tǒng)設(shè)定還原為全部自動獲??;否則程序退出:') 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 '切換成功!重置網(wǎng)絡(luò)后即可生效' 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 '切換成功!重置網(wǎng)絡(luò)后即可生效' else: print '用戶手動取消,程序退出' exit('')
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
python中的split()函數(shù)和os.path.split()函數(shù)使用詳解
今天小編就為大家分享一篇python中的split()函數(shù)和os.path.split()函數(shù)使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12win10下opencv-python特定版本手動安裝與pip自動安裝教程
這篇文章主要介紹了win10下opencv-python特定版本手動安裝與pip自動安裝教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03python開發(fā)之字符串string操作方法實例詳解
這篇文章主要介紹了python開發(fā)之字符串string操作方法,以實例形式較為詳細(xì)的分析了Python針對字符串的轉(zhuǎn)義、連接、換行、輸出等操作技巧,需要的朋友可以參考下2015-11-11