The example in this article describes how Python generates a local IP address in bulk. 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 ipconfig
#!/usr/bin/python2.7#-*-coding:utf-8-*-# Filename:AddIPAliases.pyimport re,sys,socket,struct # 1. Determine if the IP address is legitimate; 2. Determine if 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 (Ipclassesin t[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# Determine if the number entered is French def Checkipcount (ipcount): Regexipcount=re.compile (' \d+ ') checking=regexipcount.match (ipcount) if checking==none:i Pcount=raw_input ("Pro! Please enter a valid quantity: ") return ChEckipcount (ipcount) Else:return ipcount# generates a specified number of Ipdef makeips (Ipint,ipcount,ipintbool) for the User: Targetbat=open (' Addipaliases.bat ', ' w+ ') targetcsv=open (' Addipaliases.csv ', ' w+ ') # Determine if the number of IP addresses is in class A,class B or Class C in the category if int (Ipcou NT) > (ipclassesint[ipintbooltrue[1][2]]-ipint+1): Ipcount=raw_input ("Pro! The number of IPs you have entered exceeds the category "+ipintbooltrue[0]+", please re-enter: "Return Makeips (Ipint,ipcount,ipintbool) else:for I in range (int (Ipcoun T)): Ipinttrans=socket.inet_ntoa (Struct.pack ("! L ", Ipint)) ipint+=1 targetbat.write (' netsh interface ip add address ' local 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 you run the script, move to: Control Panel, view network--Local Area Connection, properties->ipv4, change the auto-get IP to manual 2. IP is divided into three categories: class A: Quantity 16777216, Range 10.0.0.0-10.255.255.255 class B: Quantity 1048576, Range 172.16.0.0-172.31.255.255 class C: Quantity 65536 , the range 192.168.0.0–192.168.255.255!!! So when you enter the IP, pleaseEnsure that the IP you enter is among these three categories!!! 3. If you are using an English system, change the "Local Area Connection" in the Custom Function makeips () to "regional Connection". 4. The script is a bit long because of the range () of the long integer data, wait for the prompt to be successful. 5. The directory where the script is stored will generate a bat file and a CSV file, and the bat file is used to add IP to the system, run it manually after the build succeeds, and the CSV file is used to invoke these IPs in JMeter. 6. If you want to clear the IP that is inserted in the system, move to: Control Panel, view network---local connection, properties->ipv4, change the manually acquired IP to get ip*********************************** automatically "# Converts the IP addresses of each class starting and ending to 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 ("Please enter start IP address:") ipcount=raw_input ("Please enter the number of IP generated:") # Determine if 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.key S () [i]) ipintbooltrue.append (ipintbool.values () [i]) break# call function generates IP address for user makeips (ipint,ipcount,ipintbooltrue) print "Bat file is finished, move to the folder where you stored the script to locate and run the file. "
"Recommended"
1. Java get local IP method
2. How the WIN10 system can view its own IP address
3. Detailed description of and and or actual usage in Python
4. Sharing the example of and/or in Python tutorial