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

# -*- coding: utf-8 -*- 
# Filename:
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):
  if Checking==None:
    IP=raw_input(" Pro! Please enter legal IP Address, : ")
    return CheckIP(IP,IPClassesInt)
    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):
  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)
    for i in range(int(IPCount)):
      IPIntTrans=socket.inet_ntoa(struct.pack("!L", IPInt))
      targetBat.write('netsh interface ip add address " Local connection " '+IPIntTrans+' '+IPIntBoolTrue[1][1]+'n')
# Main  function 
print '''
 The following information can help you run the script better: 
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, -
  Class B Number: 1048576 .   The scope of -
  Class C Number: 65536 .    The scope of  �
!!! 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 
for i in range(len(IPClasses)):
#  User input 
IP=raw_input(" Please enter the beginning IP Address: ")
IPCount=raw_input(" Please enter generated IP Quantity: ")
#  Determine whether the input is valid 
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)
for i in range (len(IPIntBool)):
  if True in IPIntBool.values()[i]:
#  The calling function is generated for the user IP address 
print "Bat After the file is generated, move to the folder where the script is stored to find and run the file. "

I hope this article has helped you with your Python programming.

Related articles: