生成 IPv4 范围内的所有 IP 地址

发布于 2024-08-17 05:25:08 字数 58 浏览 5 评论 0原文

生成所有可能的 IPv4 地址的有效方法是什么?除了迭代一个巨大的嵌套 for 循环中的所有字节之外。

What would be a efficient way to generate all possible IP v4 addresses? other than iterating all bytes in a one giant nested for loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里兽 2024-08-24 05:25:08

编辑:我之前的答案会从 128.0.0.0255.255.255.2550.0.0.0 到 <代码>127.255.255.255。大概您想从 0.0.0.0255.255.255.255,所以我编辑了我的解决方案来做到这一点。

int i = -1;
do {
  i++;

  int b1 = (i >> 24) & 0xff;
  int b2 = (i >> 16) & 0xff;
  int b3 = (i >>  8) & 0xff;
  int b4 = (i      ) & 0xff;

  //Now the IP is b1.b2.b3.b4

} while(i != -1);

注意:如果您对这个循环将如何结束感到困惑(即如何将 1 与 -1 相加足够多次使其再次变为 -1),请阅读 二进制补码。基本上,将 Integer.MAX_VALUE 加 1 会产生 Integer.MIN_VALUE,并且不会引发任何类型的异常。


旧答案。仍然命中所有 IP,但可能不是按照您想要的顺序:

for(long n = Integer.MIN_VALUE; n <= Integer.MAX_VALUE; n++)
{
  int i = (int)n;

  int b1 = (i >> 24) & 0xff;
  int b2 = (i >> 16) & 0xff;
  int b3 = (i >>  8) & 0xff;
  int b4 = (i      ) & 0xff;

  //Now the IP is b1.b2.b3.b4
}

请注意:如果循环控制变量是 int 而不是 long,这将是一个无限循环(因为所有 int 始终是 <= Integer.MAX_VALUE)。

Edit: My previous answer would have gone from 128.0.0.0 to 255.255.255.255 to 0.0.0.0 to 127.255.255.255. Presumably you want to go from 0.0.0.0 to 255.255.255.255, so I've edited my solution to do that.

int i = -1;
do {
  i++;

  int b1 = (i >> 24) & 0xff;
  int b2 = (i >> 16) & 0xff;
  int b3 = (i >>  8) & 0xff;
  int b4 = (i      ) & 0xff;

  //Now the IP is b1.b2.b3.b4

} while(i != -1);

Note: if you're confused how this loop will ever end (i.e. how adding 1 to -1 enough times makes it -1 again), read up on two's complement. Basically, adding one to Integer.MAX_VALUE results in Integer.MIN_VALUE, and does not throw any kind of exception.


Old answer. Still hits all IPs, but probably not in the order you desire:

for(long n = Integer.MIN_VALUE; n <= Integer.MAX_VALUE; n++)
{
  int i = (int)n;

  int b1 = (i >> 24) & 0xff;
  int b2 = (i >> 16) & 0xff;
  int b3 = (i >>  8) & 0xff;
  int b4 = (i      ) & 0xff;

  //Now the IP is b1.b2.b3.b4
}

Please note: If the loop control variable was an int instead of a long, this would be an infinite loop (since all ints are always <= Integer.MAX_VALUE).

梦幻的心爱 2024-08-24 05:25:08

并非所有 IPv4 地址都是有效的,具体取决于它们的用途。请参阅此处有关保留地址块和链接 RFC 的部分:http://en.wikipedia.org/wiki /IPv4

因此,根据您想要执行的操作,您可能需要检查保留地址并将其保留。

Not all IPv4 addresses are valid, depending on what purpose they're intended for. See the section on reserved address blocks and the linked RFCs here: http://en.wikipedia.org/wiki/IPv4

So depending on what you want to do, you might need to check for reserved addresses and leave them out.

若水般的淡然安静女子 2024-08-24 05:25:08

一切皆有可能? 0.0.0.0 到 255.255.255.255,即 0 到 0xFFFFFFFF

All Possible? 0.0.0.0 to 255.255.255.255 which is 0 to 0xFFFFFFFF

心的憧憬 2024-08-24 05:25:08

您可以从初始化为零的无符号 int/long(32 位数据类型)开始,并不断递增,直到达到 0xffffffff。

增量运算符通常比嵌套循环稍微高效一些。

使用位掩码和位移运算符提取您感兴趣的任何给定字节。

You can start with an unsigned int/long (32-bit data type) initialized to zero and keep incrementing until you reach 0xffffffff.

The increment operator is usually slightly more efficient than nested loops.

Use bit masks and bit shift operators to pull out any given byte that you are interested in.

只怪假的太真实 2024-08-24 05:25:08

就“效率”而言,我认为没有比循环遍历所有可能值更好的方法了。

请注意两件事:
1.地址很多,效率不会那么高。
2. 并非所有 IP 地址都是有效的(并且有很多地址您可能不打算查看)。

有关哪些 IP 地址有效的示例,请注意 224.0.0.0 和 239.255.255.255 之间的所有地址都是多播地址,所有以 127.xxx 开头的地址都是无效的,等等。

In terms of "efficiency", I don't think there is a much better way than looping through all possible values.

Do take note of two things:
1. There are a lot of addresses, so it won't be that efficient.
2. Not all IP addresses are valid (and there are plenty of addresses which you probably don't intend to go over).

For an example of what IP addresses are valid, take note that all addresses between 224.0.0.0 and 239.255.255.255 are multicast addresses, all addresses starting with 127.x.x.x are invalid, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文