用于计算插页式IP范围的库或工具?

时间:2017-04-18 12:04:24

标签: ip-address

我想找到一个库或工具,以便于在范围内枚举IP地址块。假设我有两个不重叠的范围,我想找出完全覆盖它们之间的间隙空间的最小IP地址块集。

以下是一些例子:

介于0.0.0.0/8和10.0.0.0/8之间(不包括边)谎言:1.0.0.0 / 8,2.0.0.0 / 7,4.0.0.0 / 6和8.0.0.0/7,占用全部空间,这是最小的表达。

在10.0.0.0/8和127.0.0.0/8之间(不包括边)谎言:11.0.0.0 / 8,12.0.0.0 / 6,16.0.0.0 / 4,32.0.0.0 / 3,44.0.0.0 / 3,96.0.0.0/4,112.0.0.0/5,120.0.0.0/6,124.0.0.0/7和126.0.0.0/8,占用了所有空间,这是最小的表达。

在127.0.0.0/8和169.254.0.0/16之间(不包括边)谎言:128.0.0.0 / 3,160.0.0.0 / 5,168.0.0.0 / 8,169.0.0.0 / 9,169.128.0.0 / 10,169.192.0.0 / 11,169.224.0.0 / 12,169.240.0.0 / 13,169.248.0.0 / 14和169.252.0.0/15,它占据了所有空间,是最小的表达。

目前我手动计算这个,但它确实变得乏味且容易出错。我希望听到一些函数,可能是任何编程语言,可能属于子网计算器,或者没有网页,以帮助计算这个。在我写自己的功能之前,我以为我会问。

2 个答案:

答案 0 :(得分:0)

我在Python中找到了我自己的问题的答案:

from netaddr import *

iprange_to_cidrs(IPAddress(IPSet(['0.0.0.0/8']).iprange().last + 1), IPAddress(IPSet(['10.0.0.0/8']).iprange().first - 1))
iprange_to_cidrs(IPAddress(IPSet(['10.0.0.0/8']).iprange().last + 1), IPAddress(IPSet(['127.0.0.0/8']).iprange().first - 1))
iprange_to_cidrs(IPAddress(IPSet(['127.0.0.0/8']).iprange().last + 1), IPAddress(IPSet(['169.254.0.0/16']).iprange().first - 1))

只有一个CIDR的简单范围也可以通过iprange_to_cidrs()解析,如related answer所示。

答案 1 :(得分:0)

您可以使用IPAddress Java library通过以下代码在Java中执行此操作。此代码适用于IPv4和IPv6。免责声明:我是该库的项目经理。

def Decimaltobianry(Decimal):
    print("Converted to binary " + bin(Decimal))

def Decimaltohex(Decimal):
    print("Converted to hexadecimal " + hex(Decimal))

def numberConverter():
    condition=1
    while condition==1:
        print("1. Converting a decimal number to binary")
        print("2. Converting a decimal number to hexadecimal")
        print("3. Exit the program")
        print("")
        Options=str(input("Select one of the options"))
#if statements
        if(Options==("1")):
            Decimal=int(input("Enter a number from 1 to 1000"))
            print("")
            if Decimal in range(1, 1001):
                print("The decimal number " + str(Decimal) + " is in range ")
                Decimaltobianry(Decimal)
                print("")
            else:
                print("The decimal number entered is outside the given range. Choose your options again!.")
                print("")
        if (Options == ("2")):
            Decimal = int(input("Enter a number from 1 to 1000"))
            print("")
            if Decimal in range(1, 1001):
                print("The decimal number " + str(Decimal) + " is in range ")
                Decimaltohex(Decimal)
                print("")
            else:
                print("The decimal number entered is outside the given range. Choose your options again!.")
                print("")
        else:
            print("End of Program!")
            break




numberConverter()

输出:

static void print(String lower, String upper) {
    IPAddress lowerAddr = new IPAddressString(lower).getAddress();
    IPAddress upperAddr = new IPAddressString(upper).getAddress();
    System.out.println("Between " + lowerAddr + " and " + upperAddr + ": " +
        Arrays.asList(spanInBetween(lowerAddr, upperAddr)));
}
    
static IPAddress[] spanInBetween(IPAddress lower, IPAddress upper) {
    return lower.getUpper().increment(1).
        spanWithPrefixBlocks(upper.increment(-1));
}