Python implements automatic ip address replacement

Source: Internet
Author: User
Tags nameserver
This article mainly introduces how to implement automatic ip address replacement in python. it involves the related operation skills of Python for local network configuration, which is of great practical value, for more information about how to use python to automatically replace ip addresses, see the following example. Share it with you for your reference. The specific implementation method is as follows:

#! /Usr/bin/env python #-*-encoding: gb2312-*-# Filename: IP. pyimport sitecustomizeimport _ winregimport ConfigParserfrom ctypes import * print 'is checking the network adapter. please wait... 'Printnetdomaininstanceid = Nonehkey = _ winreg. openKey (_ winreg. HKEY_LOCAL_MACHINE, \ r 'system \ CurrentControlSet \ Control \ Class \ {4d36e972-e325-11ce-bfc1-08002be10318} ') keyInfo = _ winreg. queryInfoKey (hkey) # Find the adapter name corresponding to the NIC netmask instanceidfor index in range (keyInfo [0]): hSubKeyName = _ winreg. enumKey (hkey, index) hSubKey = _ winreg. openKey (hkey, hSubKeyName) try: hNdiInfKey = _ winreg. openKey (hSubKey, r'ndi \ Interfa Ces ') lowerRange = _ winreg. queryValueEx (hNdiInfKey, 'lowerrange') # check whether it is ethernet if LowerRange [0] = 'ethernet ': driverDesc = _ winreg. queryValueEx (hSubKey, 'driverdesc') [0] print 'network adapter name detected:', driverdescnetw.instanceid = _ winreg. queryValueEx (hSubKey, 'netdeskinstanceid') [0] print 'network adapter ID:', netdeskinstanceidif netdeskinstanceid = None: print 'no network adapter found, the program exits 'exit () break_winreg.CloseKey (hNdiInfKey) Wi-Fi NdowsError: print r'message: No Ndi \ Interfaces key' # The loop ends. Currently, only one nic ip address can be modified. closeKey (hSubKey) _ winreg. closeKey (hkey) # By modifying the registry, set IPstrKeyName = 'system \ CurrentControlSet \ Services \ Tcpip \ Parameters \ Interfaces \ '+ netdeskinstanceidprint'. the registry address of the network adapter is: \ n', strKeyNamehkey = _ winreg. openKey (_ winreg. HKEY_LOCAL_MACHINE, \ strKeyName, \ 0, \ _ winreg. KEY_WRITE) config = ConfigParser. configParser () printpr Int' is opening the IP. ini configuration file... '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 'the following information is set in the configuration file. please check: 'printprint 'IP Address :', IPAddressprint 'sub-off mask: ', SubnetMaskprint 'default Gateway:', gatewayprint' Primary DNS service Server: ', DNSServer1print' secondary DNS server: ', DNSServer2printres = raw_input (' Now, please decide: Enter 1 to write the configuration file to the system; input 2, the existing system settings are restored to all automatically obtained; otherwise, the program exits: ') if str (res) = '1': try: _ winreg. setValueEx (hkey, 'enablesdhcp ', 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) before T WindowsError: print 'set IP error' exit () _ winreg. closeKey (hkey) print 'switch successful! After the network is reset, 'Elif str (res) = '2': try: _ winreg. setValueEx (hkey, 'enablesdhcp ', 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, 'aseobtainedtime', None, _ winreg. REG_DWORD, 0x00000000) _ winreg. setValueEx (hkey, 'leaseterminatestime ', None, _ winreg. REG_DWORD, 0x00000000) handle T WindowsError: print 'set IP error' exit () _ winreg. closeKey (hkey) print 'switch successful! After the network is reset, the 'else: print' user can be manually canceled and the program exits 'exit ('')

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.