Check the dns lookup of a nameserver in Java

2019-06-08 11:37发布

Given a domain and a nameserver ip I'd like to know where is that nameserver resolving the IP in java, how can I achieve it? Thanks

标签: java http dns
2条回答
smile是对你的礼貌
2楼-- · 2019-06-08 12:05

If you need to query a specific nameserver to see how it is responding you can use the JNDI interface.

查看更多
闹够了就滚
3楼-- · 2019-06-08 12:12

You have at least two options:

If your code has to run on any VM, you must use one of the many available Java DNS libraries. Googling for "java dns library" will give you many options.

If your code is only going to run on a Sun/Oracle VM, you can use the proprietary JNDI DNS provider like this:

Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url",    "dns://<your DNS server>");

DirContext ictx = new InitialDirContext(env);
Attributes attrs = ictx.getAttributes("www.heise.de", new String[] {"A", "AAAA"});

NamingEnumeration<? extends Attribute> e = attrs.getAll();
while(e.hasMoreElements()) {
    Attribute a = e.next();
    System.out.println(a.getID() + " = " + a.get());
}

This example will query the specified DNS server for all A and AAAA records for the host www.heise.de:

A = 193.99.144.85

AAAA = 2a02:2e0:3fe:100::7

查看更多
登录 后发表回答