This article mainly introduces how to generate local IP addresses in python in batches. The example analyzes how to generate local IP addresses and bind them to the network card in Python, which has some reference value, for more information about how to generate local IP addresses in python in batches, see the following example. Share it with you for your reference. The specific analysis is as follows:
This code is used to generate a local IP address on the local computer and bind it to the NIC. a bat batch file is generated. run this batch file and you can view it through ipconfig.
#! /Usr/bin/python2.7 #-*-coding: UTF-8-*-# Filename: AddIPAliases. pyimport re, sys, socket, struct #1. determine whether the IP address is valid. 2. determine whether the IP address entered by the user is in def CheckIP (IP, IPClassesInt) in Class A, Class B, or Class C: regexIP = re. compile ('^ ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) $ ') Checking = regexIP. match (IP) if Checking = None: IP = raw_input ("Dear! 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 (IPClassesInt [2], IPClassesInt [3] + 1) and IPInt not in range (IPClassesInt [4], IPClassesInt [5] + 1): IP = raw_input ("Dear! The IP address you entered is not in Class A, Class B, or Class C. Read the prompt and re-enter the IP address: ") return CheckIP (IP, IPClassesInt) else: return IP # determine whether the entered number is valid def CheckIPCount (IPCount): regexIPCount = re. compile ('\ d +') Checking = regexIPCount. match (IPCount) if Checking = None: IPCount = raw_input ("Dear! Enter a valid quantity: ") return CheckIPCount (IPCount) else: return IPCount # generate a specified number of IPdef MakeIps (IPInt, IPCount, IPIntBool) for the User ): targetBat = open ('addipaliases. bat ', 'W +') targetcsv1_open('addipaliases.csv ', 'W +') # determine whether the number of IP addresses is in the category of Class A, Class B, or Class C if int (IPCount)> (IPClassesInt [IPIntBoolTrue [1] [2]-IPInt + 1): IPCount = raw_input ("Dear! The number of IP addresses you entered exceeds the "+ IPIntBoolTrue [0] +" category. enter ") return MakeIps (IPInt, IPCount, IPIntBool) else: for I in range (int (IPCount): 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 better run this script: 1. before running the script, go to Control Panel> view Network> Local connection> Properties> IPv4 to change the automatically obtained IP address to manual 2. there are three types of IP addresses: Class A: quantity: 16777216, range: 10.0.0.0-10.20.255 Class B: quantity: 1048576, range: 172.16. 0.0-172.31.0000255 Class C: quantity: 65536, range: 192.168.0.0-192.168.255.255 !!! When you enter an IP address, make sure that the IP address you entered belongs to these three categories !!! 3. if you are using an English system, change "Local Connection" in the custom function MakeIPs () to "Local Area Connection ". 4. because the script requires range () for long integer data, the calculation time is a bit long. please wait until the prompt is successful. 5. A bat file and a csv file will be generated in the directory where the script is stored. the bat file is used to add an IP address to the system. after the file is generated, run it manually, csv files are used to call these IP addresses in Jmeter. 6. to clear the IP address inserted in the system, go to Control Panel> view Network> Local connection> Properties> IPv4, change Manual IP address acquisition to automatic IP address acquisition ******************************** * ***************** ''' # Convert the IP addresses of each Class to integers IPClasses = ['10. 0.0.0 ', '10. limit 255', '2017. 16.0.0 ', '2017. 31.0000255 ', '2017. 168.0.0 ', '2017. 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 ("Enter the number of generated IP addresses: ") # determine whether the input is valid. 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, '2017. 240.0.0 ', 3], "ClassC": [IPIntClassCBool, '2017. 255.0.0 ', 5]} IPIntBoolTrue = [] for I in range (le N (IPIntBool): if True in IPIntBool. values () [I]: IPIntBoolTrue. append (IPIntBool. keys () [I]) IPIntBoolTrue. append (IPIntBool. values () [I]) break # Call the function to generate the IP address MakeIps (IPInt, IPCount, IPIntBoolTrue) print for the user. "The Bat file has been generated, go to the script folder to find and run the file."
I hope this article will help you with Python programming.