我正在尝试使用digg和使用Java在linux shell中再次查询特定的dns服务器。 dig命令有效。但java方式没有。怎么了?
dig @dns.nic.it test.it ;; QUESTION SECTION: ;test.it. IN A ;; AUTHORITY SECTION: test.it. 10800 IN NS dns2.technorail.com. test.it. 10800 IN NS dns.technorail.com. test.it. 10800 IN NS dns3.arubadns.net. test.it. 10800 IN NS dns4.arubadns.cz.
java方式
public static void rootDNSLookup(String domainName) throws NamingException{ String server="dns://dns.nic.it"; Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url", server); DirContext ictx = new InitialDirContext(env); Attributes attrs = ictx.getAttributes(domainName, new String[] {"A", "AAAA", "NS", "SRV"}); System.out.println(attrs); NamingEnumeration e = attrs.getAll(); while(e.hasMoreElements()) { Attribute a = e.next(); System.out.println(a.getID() + " = " + a.get()); } }
java prints:
No attributes
答案 0 :(得分:1)
我知道这已经很长时间了,但对于我们其余的人一直在寻找答案,最好的方法是使用库 dnsjava 。 您要查找的特定课程是 SimpleResolver 。它有两个构造函数。如果没有参数使用,它将使用系统的DNS设置。如果您提供IP地址,它将强制它作为DNS服务器。 您可以在这里找到一个很好的用法示例: https://github.com/dnsjava/dnsjava/blob/master/dig.java