gpt4 book ai didi

java - 使用通配符 IP 地址获取域名

转载 作者:太空宇宙 更新时间:2023-11-04 06:45:44 27 4
gpt4 key购买 nike

我正在尝试编写一个类,该类将从/etc/hosts 文件中的 IP 地址返回域名。例如,我知道 Google 拥有的 IP 地址 block 是 74.125.0.0 - 74.125.255.255 并且在我的主机文件中;

74.125.24.155   Google
74.125.24.101 Google
74.125.24.102 Google
74.125.24.132 Google
74.125.24.113 Google
74.125.24.84 Google

我编写了一段简单的代码,它将查找主机文件并搜索相应 IP 地址的名称。这是代码;

String destination = "74.125.24.84";
InetAddress address = InetAddress.getByName(destination);
String resolvedHost = address.getHostName();
System.out.println("Translated " + destination + " to host name " + resolvedHost);

这将返回以下Translated 74.125.24.84 to host name Google。然后我尝试了;

String destination = "74.125.24.*";
InetAddress address = InetAddress.getByName(destination);
String resolvedHost = address.getHostName();
System.out.println("Translated " + destination + " to host name " + resolvedHost);

但响应中出现以下错误;

java.net.UnknownHostException: 74.125.24.*
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getByName(Unknown Source)
at IPToDomainName.main(IPToDomainName.java:12)

是否可以使用通配符来表示 Google 的 IP 地址?

最佳答案

你能迭代它吗?

for (int i=0;i<=255;i++) {
String destination = "74.125.24." + i;
InetAddress address = InetAddress.getByName(destination);

if (address == null) // Or any exception.
continue;

String resolvedHost = address.getHostName();

System.out.println("Translated " + destination + " to host name " + resolvedHost);
}

关于java - 使用通配符 IP 地址获取域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24019504/

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