【问题标题】:Using a wildcard IP Address to get a Domain Name使用通配符 IP 地址获取域名
【发布时间】:2019-06-19 17:19:56
【问题描述】:

我正在尝试编写一个类,该类将从我的 /etc/hosts 文件中的 IP 地址返回域名。例如,我知道 Google 拥有的 IP 地址块是 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

我编写了一段简单的代码,它将查看 hosts 文件并搜索相应 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 地址?

【问题讨论】:

  • 为什么需要这样做?这与域名试图实现的目标完全相反。我怀疑如果你描述你的实际目标,会有更好的方法来实现它。
  • 我们有一个路由器,它收集连接到 WiFi 的人们正在查看的网站的 IP 地址。从中我们可以绘制图表,显示有多少人访问了哪个网站,但结果仅显示 IP 地址。我已经编辑了 /etc/hosts 文件,但总是有更多的 IP 地址
  • 为什么不将收集到的 ip 地址提供给 prog 并获取主机名列表来创建图表
  • 恐怕这不在我的掌控之中,我只是一台大机器中的一个小齿轮,只是试图解决给我的这个问题
  • @dan 你为什么要把东西放在主机文件中?您的代码应该在不将任何内容放入主机文件的情况下执行反向 DNS。

标签: java ip


【解决方案1】:

你能遍历它吗?

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);
}

【讨论】:

  • 您忘记在该循环中使用i。我怀疑你的意思是"74.125.24." + i;。至少根据问题,您可能还需要包含 255 值。
  • 在gethostname之前检查地址是否为空:)
猜你喜欢
  • 2011-01-28
  • 2023-04-08
  • 1970-01-01
  • 2011-04-04
  • 2018-04-10
  • 2012-05-11
  • 2011-06-12
  • 2018-01-02
  • 1970-01-01
相关资源
最近更新 更多