Python's method for bulk generation of local IP addresses

• 2020-04-02 14:43:23
• OfStack

This article illustrates a python method for bulk generation of local IP addresses. Share with you for your reference. Specific analysis is as follows:

This code is used to generate the local IP address bound to the network card on the local computer, and it generates a bat batch file, which is run and can be viewed through ipconfig

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import re,sys,socket,struct
# 1.  judge IP Whether the address is legal;  2.  Determine what the user entered IP Whether in Class A . Class B  or  Class C In the
def CheckIP(IP,IPClassesInt):
regexIP=re.compile('^([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5])\$')
Checking=regexIP.match(IP)
if Checking==None:
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(IPClassesInt[2],IPClassesInt[3]+1) and IPInt not in range(IPClassesInt[4],IPClassesInt[5]+1):
IP=raw_input(" Pro! You enter the IP Address is not Class A . Class B or Class C , please read the prompt message and enter it again IP Address: ")
return CheckIP(IP,IPClassesInt)
else: return IP
#  Determine whether the number entered is valid
def CheckIPCount(IPCount):
regexIPCount=re.compile('d+')
Checking=regexIPCount.match(IPCount)
if Checking==None:
IPCount=raw_input(" Pro! Please enter the legal quantity: ")
return CheckIPCount(IPCount)
else: return IPCount
#  Generates a specified number of IP
def MakeIps(IPInt,IPCount,IPIntBool):
#  judge IP Is the number of addresses in Class A . Class B or Class C The category of
if int(IPCount)>(IPClassesInt[IPIntBoolTrue[1][2]]-IPInt+1):
IPCount=raw_input(" Pro! You enter the IP outnumber "+IPIntBoolTrue[0]+" , please re-enter: ")
return MakeIps(IPInt,IPCount,IPIntBool)
else:
for i in range(int(IPCount)):
IPIntTrans=socket.inet_ntoa(struct.pack("!L", IPInt))
IPInt+=1
targetCSV.write(IPIntTrans+'n')
targetBat.write('pause')
# Main  function
print '''
**************************************************

1.  Before running the script, move to the control panel -> Check the network -> Local connection -> attribute ->IPv4 , will be automatically obtained IP To manually
2. IP Divided into three categories:
Class A Number: 16777216 Range, 10.0.0.0 - 10.255.255.255
Class B Number: 1048576 .   The scope of 172.16.0.0 - 172.31.255.255
Class C Number: 65536 .    The scope of 192.168.0.0  �  192.168.255.255
!!! So you're typing IP When, please make sure that you enter IP Falls into one of these three categories !!!
3.  If you use an English system, please customize the function MakeIPs() Change "local connection" to" Local Area Connection ".
4.  The script is due to the long integer data range() , so the calculation time is a little long, please wait for the success prompt.
5.  One will be generated in the directory where the script resides bat Files and csv File, bat The file is used to add to the system IP , please run it manually after successful generation, csv Files are used in Jmeter And call these IP .
6.  If you want to clear the insert in the system IP , please move to the control panel -> Check the network -> Local connection -> attribute ->IPv4 , will be obtained manually IP So let's say auto fetch IP
**************************************************
'''
#  Will all Class Beginning and ending IP Convert the address to an integer
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
IPCount=raw_input(" Please enter generated IP Quantity: ")
#  Determine whether the input is valid
IPCount=CheckIPCount(IPCount)
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(IPIntBool.values()[i])
break
#  The calling function is generated for the user IP address
MakeIps(IPInt,IPCount,IPIntBoolTrue)
print "Bat After the file is generated, move to the folder where the script is stored to find and run the file. "