Python's approach to automatically replacing IP

Source: Internet
Author: User
Tags config ini print print nameserver

This article mainly introduces Python implementation of automatic replacement of IP methods, involving Python for the local network configuration of the relevant operational skills, very practical value, the need for friends can refer to the

This article is an example of how Python implements automatic replacement of IP. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 #!/usr/bin/env python #-*-encoding:gb2312-*-# Filename:IP.py Import sitecustomize import _winreg import Configparser The From ctypes Import * Print is in the network adapter detection, please wait ... ' Print Netcfginstanceid = None hkey = _winreg. Openkey (_winreg. HKEY_LOCAL_MACHINE, R ' systemcurrentcontrolsetcontrolclass{4d36e972-e325-11ce-bfc1-08002be10318} ') KeyInfo = _ WinReg. Queryinfokey (hkey) # Find the adapter name for the NIC Netcfginstanceid for index in range (Keyinfo[0]): Hsubkeyname = _winreg. EnumKey (hkey, index) Hsubkey = _winreg. Openkey (hkey, hsubkeyname) Try:hndiinfkey = _winreg. Openkey (Hsubkey, R ' ndiinterfaces ') LowerRange = _winreg. QueryValueEx (Hndiinfkey, ' LowerRange ') # Check if ethernet if lowerrange[0] = = ' Ethernet ': DriverDesc = _winreg. QueryValueEx (Hsubkey, ' DriverDesc ') [0] print ' detects the network adapter name: ', DriverDesc Netcfginstanceid = _winreg. QueryValueEx (Hsubkey, ' Netcfginstanceid ') [0] print ' detects network adapter ID: ', netcfginstanceid if Netcfginstanceid = = None:print ' No network adapter found, program exits ' exit () break _winreg. Closekey (Hndiinfkey) except Windowserror:prinT R ' message:no ndiinterfaces Key ' # Loop end, currently only provides the ability to modify a network card IP _winreg. Closekey (Hsubkey) _winreg. Closekey (hkey) # set IP strkeyname = ' systemcurrentcontrolsetservicestcpipparametersinterfaces ' + by modifying the registry The Netcfginstanceid print ' network adapter's registry address is: N ', strkeyname hkey = _winreg. Openkey (_winreg. HKEY_LOCAL_MACHINE, Strkeyname, 0, _winreg. key_write) config = Configparser.configparser () print is opening 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 = [ The following information is set up in the Dnsserver1,dnsserver2] print profile, check: ' Print print ' IP address: ', IPAddress print ' Child close mask: ', SubnetMask print ' default gateway: ', G Ateway print ' Primary DNS server: ', DNSServer1 print ' Secondary DNS server: ', DNSServer2 print res = Raw_input (' Now, please decide: Enter 1, then write the configuration file to the system; Enter 2, The existing system settings are restored to all automatic acquisition; otherwise the program exits: ' 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 ' Switch success! After resetting the network, it can take effect ' 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 ' Switch success! Reset the network can take effect ' else:print ' user manually cancel, program exit ' exit (')

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.