Python implements automatic ip address replacement, while python replaces ip addresses.

Source: Internet
Author: User
Tags nameserver

Python implements automatic ip address replacement, while python replaces ip addresses.

This example describes how to automatically replace an ip address in python. 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.