python實現自動更換ip的方法

來源:互聯網
上載者:User
本文執行個體講述了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程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.