国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現自動更換ip的方法

2020-02-23 01:05:57
字體:
來源:轉載
供稿:網友

本文實例講述了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('')            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苍南县| 蒙山县| 兴城市| 桂阳县| 历史| 铜鼓县| 望谟县| 获嘉县| 尉氏县| 奎屯市| 突泉县| 界首市| 江油市| 定日县| 瓦房店市| 乌审旗| 大洼县| 白山市| 遵化市| 志丹县| 涿鹿县| 松溪县| 合阳县| 琼中| 深圳市| 友谊县| 凤翔县| 满洲里市| 兰州市| 黔江区| 偃师市| 闽侯县| 大邑县| 濮阳县| 搜索| 赫章县| 信丰县| 龙胜| 桦甸市| 宜黄县| 德昌县|