Get name server information for DNS lookup in Java

If I searched for the IP address from the DNS name as follows:

InetAddress inetAddress = InetAddress.getByName(name);
String address = inetAddress.getHostAddress();

Can I find out which name server gave me the information?

+3
source share
3 answers

I'm not sure if this is what you want, but there is a DNSJava library there that provides DNS functions in Java. Perhaps you can use this to better understand your problems or implement a specific solution? As I said, this is not a perfect match for you, but it may be useful.

+2
source

For this InetAddress, try the following:

// get the default initial Directory Context
InitialDirContext idc = new InitialDirContext();
// get the DNS records for inetAddress
Attributes attributes = idc.getAttributes("dns:/" + inetAddress.getHostName());
// get an enumeration of the attributes and print them out
NamingEnumeration attributeEnumeration = attributes.getAll();
System.out.println("-- DNS INFORMATION --");
while (attributeEnumeration.hasMore()) {
    System.out.println("" + attributeEnumeration.next());
}
attributeEnumeration.close();

Adapt it to choose what you are looking for.

+2
source

. DNS-. , . .

final String ADDR_ATTRIB = "A";
final String[] ADDR_ATTRIBS = {ADDR_ATTRIB};
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
idc = new InitialDirContext(env);
env.put(Context.PROVIDER_URL, "dns://"+dnsServer);
final List<String> ipAddresses = new LinkedList<>();
final Attributes attrs = idc.getAttributes(hostname, ADDR_ATTRIBS);
final Attribute attr = attrs.get(ADDR_ATTRIB);
if (attr != null) for (int i = 0; i < attr.size(); i++)  ipAddresses.add((String) attr.get(i));
0

Source: https://habr.com/ru/post/1720758/


All Articles