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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:49:53  来源:igfitidea点击:

DNS query in JAVA

javadnsinetaddress

提问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 InetAddressfor this but keep on getting exception errors. Since the errors refer to 'Unknown Host' I don't think InetAddresscan 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

You cannotlookup TXT or other DNS records InetAddressclass. InetAddress.getAllByName()looks up for A, or AAAArecords only.

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

Check DNS Javafor your needs.

根据您的需要检查DNS Java

回答by user207421

InetAddressdoesn'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”。