简体   繁体   English

获取两个IP地址之间的IP地址列表

[英]Getting IP address list between two IP addresses

I am writing a program that gets From IP address and To IP address from the user and displays the list of IP addresses between them. 我正在编写一个程序,该程序从用户获取“从IP地址”和“至IP地址”,并显示它们之间的IP地址列表。 For example, if the user gives 10.0.0.1 and 10.0.0.5 then I will display the five IP addresses between these two. 例如,如果用户给出了10.0.0.1和10.0.0.5,那么我将显示这两个之间的五个IP地址。 The current solution that is coming in my mind are: 我想到的当前解决方案是:

  1. To have a list of all IP addresses and then look for the resultant IP address list 要获得所有IP地址的列表,然后查找结果IP地址列表
  2. Use a nested loop 使用嵌套循环

What solution should I adopt between these (or suggest a better solution)? 在这些之间我应该采用什么解决方案(或提出更好的解决方案)? For the first solution, what is the link for IP address table/list? 对于第一个解决方案,IP地址表/列表的链接是什么?

What is the solution in terms of JavaScript or Java? 用JavaScript或Java解决方案是什么?

First split the IP addresses with . 首先用分割IP地址. . From the first IP address, start increasing the fourth part up to 255 and then add 1 to the third part and set the fourth one to 1. Until you reach the to IP address. 从第一个IP地址开始,将第四部分增加到255,然后将1增加到第三部分,并将第四部分设置为1,直到到达IP地址为止。

  • IP address bytes -> bits -> Int32 IP地址字节->位-> Int32
  • From: 10.0.10.10 -> 00001010 00000000 00001010 00001010 -> 167774730 从: 10.0.10.10 > 00001010 00000000 00001010 00001010 > 167774730
  • To: 10.1.45.1 -> 00001010 00000001 00101101 00000001 -> 167849217 收件人: 10.1.45.1 > 00001010 00000001 00101101 00000001 > 167849217
  • Start count from From to To and just check the unwanted bytes which is 11111111 and 00000000 . 从From到To开始计数,只检查不需要的字节1111111100000000

That's all. 就这样。

The "dot"-writing is for humans. “点”字是针对人类的。 For computers, it is one 4-byte-number. 对于计算机,它是一个4字节数字。 So parse it to a number. 因此将其解析为一个数字。 Then you will get all addresses in the range by simply increasing a number until the bound is reached and format them back for output. 然后,您只需增加一个数字直到达到界限,就可以获取范围内的所有地址,然后将其格式化回输出。

I was experimenting in an updated jsFiddle , and finally I came to the solution below. 我正在尝试更新的jsFiddle ,最后来到了下面的解决方案。 The following code should work for all IP addresses. 以下代码适用于所有IP地址。 You have to provide a start and end IP address in hex (since it is easy, I did not write code for it). 您必须以十六进制形式提供起始和结束IP地址(因为这很容易,我没有为此编写代码)。

var startIp = 0x0A000001,
endIp = 0x0A000F05;

var  temp, list = [],str;
for(var i=startIp ; i <= endIp ; i++){
    temp = (i).toString(16);
    str ='';
    if(temp.length == 7){
        temp = "0"+temp;
    }

    for(var k=temp.length-1; k  >= 0 ; k-=2){
       str = parseInt(temp[k-1] + "" + temp[k], 16) +"." + str ;
    }
    document.write(temp + " " + str+ "<br>");
    list.push(str.substring(0, str.length-1));
}

?

Having a list of all IP addresses and then look for the ones you need is overkill. 列出所有IP地址,然后查找所需的IP地址实在是太过分了。 Plus, I'm sure you'll even be able to store that list without having to buy a few extra hard disk drives. 另外,我相信您甚至可以在无需购买一些额外硬盘驱动器的情况下存储该列表。

Just use a nested loop and generate the IP addresses you're looking for. 只需使用嵌套循环并生成您要查找的IP地址即可。

If you know enough to get the addresses (from the network or from the file system or user input), you can test the address itself with subtraction and get the number of IP addresses right there. 如果您知道足够的地址(从网络,文件系统或用户输入)获取地址,则可以通过减法测试地址本身,并在那里获取IP地址的数量。

This is simplified, but you will get it if you know about addresses: 000044-000002 = 000042 . 这是简化的,但是如果您知道地址: 000044-000002 = 000042 ,则可以得到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM