Python method for batch generation of local IP addresses _python

Source: Internet
Author: User
Tags get ip ip number unpack

This example describes how Python generates local IP addresses in batches. Share to everyone for your reference. The specific analysis is as follows:

This code is used to generate a local IP address on the local computer to bind to the network card, generate a bat batch file, run this batch file, can be viewed through the ipconfig

#!/usr/bin/python2.7 #-*-Coding:utf-8-*-# Filename:AddIPAliases.py Import re,sys,socket,struct # 1. Determine if the IP address is legal; 2. Determines whether the IP entered by the user is in class A,class B or Class C, Def checkip (ip,ipclassesint): Regexip=re.compile (' ^ [01]?\d\d?| 2[0-4]\D|25[0-5]) \. ([01]?\d\d?| 2[0-4]\D|25[0-5]) \. ([01]?\d\d?| 2[0-4]\D|25[0-5]) \. ([01]?\d\d?| 2[0-4]\D|25[0-5]) $ ' checking=regexip.match (IP) if Checking==none:ip=raw_input ("Pro!") Please enter a valid IP address ha: ") return Checkip (ip,ipclassesint) else:ipint=struct.unpack ('! L ', Socket.inet_aton (IP)) [0] if Ipint not in range (ipclassesint[0],ipclassesint[1]+1) and Ipint not in range (Ipclassesi nt[2],ipclassesint[3]+1) and Ipint not in range (ipclassesint[4],ipclassesint[5]+1): Ip=raw_input ("Pro!") The IP address you entered is not in class A,class B or Class C, please read the prompt and re-enter the IP address: ") return Checkip (ip,ipclassesint) Else:return IP # To determine the number entered is legal def Checkipcount (Ipcount): Regexipcount=re.compile (' \d+ ') checking=regexipcount.match (ipcount) if Checking==Non E:ipcount=raw_input ("Kiss!") PleaseEnter legal quantity: ") return Checkipcount (Ipcount) Else:return Ipcount # Generates a specified number of IP def makeips (ipint,ipcount,ipintbool) for the user: Targetbat=open (' Addipaliases.bat ', ' w+ ') targetcsv=open (' Addipaliases.csv ', ' w+ ') # Determine whether the number of IP addresses is in class A,class B or Class The category of C if int (Ipcount) > (ipclassesint[ipintbooltrue[1][2]]-ipint+1): Ipcount=raw_input ("Pro!") You have entered more IP than the "+ipintbooltrue[0]+" category, Please re-enter: "") return Makeips (Ipint,ipcount,ipintbool) else:for i-in range int (IPC Ount): Ipinttrans=socket.inet_ntoa (Struct.pack) ("! L ", Ipint)) ipint+=1 targetbat.write (' netsh interface ip add address ' Local Area Connection ' +ipinttrans+ ' +ipintbooltrue[1][ 1]+ ' \ n ') targetcsv.write (ipinttrans+ ' \ n ') targetbat.write (' pause ') # Main function print ' *************************** The following information can help you run this script better: 1. Before running the script, please move to: Control Panel-> View Network-> Local connection-> properties->ipv4, automatically get IP to manually 2. IP is divided into three categories: Class A: Number 16777216, Range 10.0.0.0-10.255.255.255 class B: Number 1048576, Range 172.16.0.0-172.31.255.255 class C: Number 65 536,  Range 192.168.0.0–192.168.255.255!!!
So when you enter IP, please ensure that the IP you entered belongs to these three categories!!!
3. If you are using the English system, please change the "local connection" in the Custom Function makeips () to "Connection".
4. Because the script is to range () for long integer data, the calculation time is a bit long, please wait for the success prompt.
5. The directory stored in the script will generate a bat file and a CSV file, the bat file is used to add IP to the system, the build is successful, run it manually, CSV files are used to invoke these IPs in JMeter. 6. If you want to clear the IP inserted in the system, please move: Control Panel-> View the network-> Local connection-> properties->ipv4, will manually obtain IP to automatically obtain IP *********************************** "# Converts the start and end IP addresses of each class into integers ipclasses=[' 10.0.0.0 ', ' 10.255.255.255 ', ' 172.16.0.0 ', ' 172.31.255.255 ', ' 192.168.0.0 ', ' 192.168.255.255 '] ipclassesint=[for I in range (len (ipclasses)): Ipclassesint.append (Struct.unpack ('!)
L ', Socket.inet_aton (Ipclasses[i])) [0]) # User input ip=raw_input ("Enter the starting IP address:") ipcount=raw_input ("Please enter the generated IP number:") # to determine whether the input is legal Ipaddress=checkip (Ip,ipclassesint) ipcount=checkipcount (ipcount) ipint=struct.unpack ('!
L ', Socket.inet_aton (IPAddress)) [0] Ipintclassabool=ipint in range (ipclassesint[0],ipclassesint[1]+1) Ipintclassbbool=ipint in range (ipclassesint[2],ipclassesint[3]+1) ipintclasscbool=ipinT in range (ipclassesint[4],ipclassesint[5]+1) ipintbool={"ClassA": [Ipintclassabool, ' 255.0.0.0 ', 1], "CLASSB": [ Ipintclassbbool, ' 255.240.0.0 ', 3], "CLASSC": [Ipintclasscbool, ' 255.255.0.0 ', 5]} ipintbooltrue=[] for I in range (Len ( Ipintbool): If True in ipintbool.values () [I]: Ipintbooltrue.append (Ipintbool.keys () [i]) ipintbooltrue.append (IP Intbool.values () [i]) Break # Call function generates IP address makeips (ipint,ipcount,ipintbooltrue) Print bat file for user Please navigate to the folder where you stored the script to locate and run the file. "

I hope this article will help you with your 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.