Home  >  Article  >  Backend Development  >  Detailed example of how to use python to generate local IPs in batches

Detailed example of how to use python to generate local IPs in batches

伊谢尔伦
伊谢尔伦Original
2017-05-31 14:21:361851browse

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