JAVA中的DNS查询

zJK*_*zJK 8 java dns inetaddress

我正在乱用Java中的DNS服务 - 我特意尝试查找所有google.com地址并将其显示在数组中,类似于使用nslookup运行查找:

nslookup -q=TXT _netblocks.google.com 8.8.8.8
Run Code Online (Sandbox Code Playgroud)

我正在使用InetAddress这个,但继续得到异常错误.由于错误引用了"未知主机",我认为InetAddress不能读取TXT记录(如果我使用google.com它可以工作,但这并不显示完整的IP范围).以下是我的代码:

InetAddress dnsresult[] = InetAddress.getAllByName("_netblocks.google.com");
            for (int i=0; i<dnsresult.length; i++)
            System.out.println (dnsresult[i]);
Run Code Online (Sandbox Code Playgroud)

如果有人能指出我正确的方向,我将不胜感激.

-JK

Man*_*ari 6

无法查找TXT或其他DNS记录InetAddress类。InetAddress.getAllByName()仅查找AAAAA记录。

检查DNS Java是否满足您的需求。


use*_*421 5

InetAddress不这样做,但您可以通过JNDI DNS provider在 Java 中完成 DNS TXT 记录查找。


use*_*346 5

这是一个可以执行您要执行的操作的示例:

Attribute attr = new InitialDirContext().getAttributes("dns:_netblocks.google.com", new String[] {"TXT"}).get("TXT");
System.out.println("attr.get() = " + attr.get());
System.out.println("attr.getAll() = " + Collections.list(attr.getAll()));
Run Code Online (Sandbox Code Playgroud)

如果您想使用自定义 dns 服务器,请改用“dns://1.1/_netblocks.google.com”。