views:

211

answers:

2

Hi:

Is there a way in Java to do a Nslookup search on SRV records?

I need to bind to Active Directory and would like to use the SRV records to help determine which of the ADs in the cluster are live.

In command line the nslookup search string is: 'nslookup -type=srv _ ldap._tcp.ActiveDirectory domain name'

Thanks.

A: 

I've never used it, but I believe that Java's "JNDI" API for naming services has a provider for DNS. Look up the domain name as a DirContext, and then examine its "SRV" attributes.

DirContext ctx = new InitialDirContext();
Attributes attrs = ctx.getAttributes("dns:/y.com", new String[] {"SRV"});

Like I say, I haven't experimented with this, but I hope it helps you get started.

erickson
A: 

As erickson pointed out, the JNDI API has a provider for DNS using JNDI, which you can read up about on that link. For a working sample query the _ldap._.tcp.mydomain.com record, see this code from Hudson.

I believe that before utilizing the DNS provider, you need to load it with something like this (modified from the Hudson code above):

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", "dns:");
DirContext ctx = new InitialDirContext(env);

from there you, can obtain the SRV record via something like:

Attributes attributes = ctx.getAttributes("_ldap._tcp.mydomain.com", new String[]{"SRV"});
Attribute a = attributes.get("SRV");

I've successfully managed to use code like this in a couple of projects for very straight-forward AD integration.

ig0774