java JAVA中的DNS查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28560135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DNS query in JAVA
提问by zJK
I am messing around with DNS services in Java - I am specifically trying to lookup all google.com addresses and display them in an array, similar to running a lookup using nslookup:
我正在使用 Java 中的 DNS 服务 - 我专门尝试查找所有 google.com 地址并将它们显示在数组中,类似于使用 nslookup 运行查找:
nslookup -q=TXT _netblocks.google.com 8.8.8.8
I am using InetAddress
for this but keep on getting exception errors. Since the errors refer to 'Unknown Host' I don't think InetAddress
can read TXT records (if I use google.com it works, but that does't show the full IP Range). Below is my code:
我正在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]);
Would appreciate it if someone can point me in the right direction.
如果有人能指出我正确的方向,我将不胜感激。
-JK
-JK
回答by Manish Maheshwari
回答by user207421
InetAddress
doesn't do this, but you can accomplish DNS TXT record lookups in Java via the JNDI DNS provider.
InetAddress
不这样做,但您可以通过JNDI DNS provider在 Java 中完成 DNS TXT 记录查找。
回答by user988346
Here is an example that does what you are trying to do:
这是一个可以执行您要执行的操作的示例:
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()));
If you want to use a custom dns server use "dns://1.1/_netblocks.google.com" instead.
如果您想使用自定义 dns 服务器,请改用“dns://1.1/_netblocks.google.com”。