search
HomeBackend DevelopmentPython TutorialDetailed 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment