gpt4 book ai didi

java - Coldfusion 获取给定 IP4 地址范围内的所有 IP 地址

转载 作者:行者123 更新时间:2023-12-01 21:54:25 25 4
gpt4 key购买 nike

构建监控工具,管理员可以输入IP范围例如。

开始:192.168.1.3 结束:192.168.1.30

输出:结构体或数组

  • 192.168.1.4
  • 192.168.1.5
  • 192.168.1.6...等等

    或者

开始:192.168.1.50 结束:192.169.1.12

输出:结构体或数组

  • 192.168.1.50
  • 192.168.1.51...
  • 192.169.1.3

我怎样才能达到这个结果。有可用的java库吗?

最佳答案

知道 IP4 地址是 32 位(4 个元素中的每一个都是 8 位),您可以执行以下操作:

  • 将两个 IP 地址转换为 32 位整数
  • 创建一个从 begin_int 迭代到 end_int 的循环
  • 将循环索引转换回 IP4 地址

(抱歉没有给出代码,但我的java知识有限)

更新(谷歌是你的 friend )(好吧,你也可以自己做!)我的灵感来自here 。正如我所说:不能保证这有效!

import java.net.InetAddress;
import java.nio.ByteBuffer;

// Convert from an IPv4 address to an integer
InetAddress from_inet = InetAddress.getByName("192.168.1.50");
int from_address = ByteBuffer.wrap(from_inet.getAddress()).getInt();


// Convert from an IPv4 address to an integer
InetAddress to_inet = InetAddress.getByName("192.169.1.12");
int to_address = ByteBuffer.wrap(to_inet.getAddress()).getInt();


for(int i = from_address; i < to_address; i++) {
// Convert from integer to an IPv4 address
InetAddress foo = InetAddress.getByName(i);
String address = foo.getHostAddress();
System.out.println(address);
}

关于java - Coldfusion 获取给定 IP4 地址范围内的所有 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34618981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com