


Detailed example of how to use python to generate local IPs in batches
The example in this article describes the method of batch generating local IP addresses in Python. Share it with everyone 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 network card. What is generated is a bat batch file. Run this batch file. You can view it through ipconfig
#!/usr/bin/python2.7 # -*- coding: utf-8 -*- # Filename: AddIPAliases.py import re,sys,socket,struct # 1. 判断IP地址是否合法; 2. 判断用户输入的IP是否在Class A,Class B 或 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("亲!请输入合法的IP地址哈: ") 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("亲!您输入的IP地址不在Class A,Class B或Class C中,请阅读提示信息并重新输入IP地址:") return CheckIP(IP,IPClassesInt) else: return IP # 判断输入的数字是否合法 def CheckIPCount(IPCount): regexIPCount=re.compile('\d+') Checking=regexIPCount.match(IPCount) if Checking==None: IPCount=raw_input("亲!请输入合法的数量:") return CheckIPCount(IPCount) else: return IPCount # 为用户生成指定数量的IP def MakeIps(IPInt,IPCount,IPIntBool): targetBat=open('AddIPAliases.bat','w+') targetCSV=open('AddIPAliases.csv','w+') # 判断IP地址的数量是否在Class A,Class B或Class C的范畴中 if int(IPCount)>(IPClassesInt[IPIntBoolTrue[1][2]]-IPInt+1): IPCount=raw_input("亲!您输入的IP数量超过了"+IPIntBoolTrue[0]+"的范畴,请重新输入:") 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 "本地连接" '+IPIntTrans+' '+IPIntBoolTrue[1][1]+'\n') targetCSV.write(IPIntTrans+'\n') targetBat.write('pause') # Main 函数 print ''' ************************************************** 以下信息能够帮助你更好的运行此脚本: 1. 在运行脚本前,请移步:控制面板->查看网络->本地连接->属性->IPv4,将自动获取IP改成手动 2. IP分为三类: Class A:数量16777216,范围10.0.0.0 - 10.255.255.255 Class B:数量1048576, 范围172.16.0.0 - 172.31.255.255 Class C:数量65536, 范围192.168.0.0 – 192.168.255.255 !!!所以大家在输入IP的时候,请保证您输入的IP属于这三个分类之中!!! 3. 如果你使用的是英文系统,请将自定义函数MakeIPs()中的“本地连接”改成“Local Area Connection”。 4. 脚本由于要对长整型的数据进行range(),所以计算时间有点长,请等待成功提示。 5. 在脚本存放的目录将会生成一个bat文件和csv文件,bat文件用于向系统中添加IP,生成成功后请手动运行它,csv文件用于在Jmeter中调用这些IP。 6. 如果想清除在系统中插入的IP,请移步:控制面板->查看网络->本地连接->属性->IPv4,将手动获取IP改成自动获取IP ************************************************** ''' # 将各个Class的起始和结束的IP地址转换成整数 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]) # 用户输入 IP=raw_input("请输入起始IP地址:") IPCount=raw_input("请输入生成的IP数量:") # 判断输入是否合法 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(IPIntBool.values()[i]) break # 调用函数为用户生成IP地址 MakeIps(IPInt,IPCount,IPIntBoolTrue) print "Bat文件生成完毕,请移步至存放的脚本的文件夹找到并运行此文件。"
[Related recommendations]
1. Java method of obtaining local IP
2 . How to check your own IP address in Win10 system
3. Detailed introduction to the actual usage of and and or in Python
4. Share Example tutorial of operation logic of and / or in python
The above is the detailed content of Detailed example of how to use python to generate local IPs in batches. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
