Saturday 29 May 2021

Java: ldapsdk: Get SRV records of a domain

When you are working with LDAP, most of the LDAP clients need to configure with the addresses of LDAP servers to use.

 

How can I get all the LDAP servers behind given ldap domain name?

Using nslookup command, you can get all the SRV records configured wih given domain name.

 

Syntax

nslookup -type=SRV _ldap._tcp.<Domain_Name>

 

Example

$ nslookup -type=SRV _ldap._tcp.ca.ab.com.com

Non-authoritative answer:
_ldap._tcp.ca.sample.com	service = 0 100 389 ds1.ca.sample.com.
_ldap._tcp.ca.sample.com	service = 0 100 389 ds2.ca.sample.com.
_ldap._tcp.ca.sample.com	service = 0 100 389 ds3.ca.sample.com.
_ldap._tcp.ca.sample.com	service = 0 100 389 ds4.ca.sample.com.
_ldap._tcp.ca.sample.com	service = 0 100 389 ds5.ca.sample.com.
_ldap._tcp.ca.sample.com	service = 0 100 389 ds6.ca.sample.com.
_ldap._tcp.ca.sample.com	service = 0 100 389 ds7.ca.sample.com.

 

How to get the SRV records in Java?

Step 1: Get an instance of DirContext

DirContext context = (DirContext) NamingManager.getURLContext("dns", new Hashtable<String, Object>());

 

Step 2: Define ldap dns url.

String ldapDNSURL = "dns:///_ldap._tcp.ca.sample.com";

 

Step 3: Get SRV records configured for this dns.

String[] attrIds = { "SRV" };
Attributes attributes = context.getAttributes(ldapDNSURL, attrIds);

Step 4: Get SRV records and print.

Attribute servers = attributes.get("SRV");

System.out.println("All the SRV records");
for (int i = 0; i < servers.size(); i++) {
	System.out.println(servers.get(i));
}


Find the below working application.

 

GetAllServersBehindDNS.java

package com.sample.app;

import java.util.Hashtable;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.spi.NamingManager;

public class GetAllServersBehindDNS {

	public static void main(String args[]) throws NamingException {
		DirContext context = (DirContext) NamingManager.getURLContext("dns", new Hashtable<String, Object>());

		String ldapDNSURL = "dns:///_ldap._tcp.ca.sample.com";
		String[] attrIds = { "SRV" };
		Attributes attributes = context.getAttributes(ldapDNSURL, attrIds);

		Attribute servers = attributes.get("SRV");

		System.out.println("All the SRV records");
		for (int i = 0; i < servers.size(); i++) {
			System.out.println(servers.get(i));
		}

		System.out.println("\nServers behind the domain name");

		for (int i = 0; i < servers.size(); i++) {
			String serverDetails = servers.get(i).toString();

			String split[] = serverDetails.split(" ");

			String server = split[3];

			int lastCharIndex = server.lastIndexOf(".");
			if (lastCharIndex == server.length() - 1) {
				server = server.substring(0, server.length() - 1);
			}

			System.out.println(server);

		}

	}

}


Output

All the SRV records
0 100 389 ds1.ca.sample.com.
0 100 389 ds2.ca.sample.com.
0 100 389 ds3.ca.sample.com.
0 100 389 ds4.ca.sample.com.
0 100 389 ds5.ca.sample.com.
0 100 389 ds6.ca.sample.com.
0 100 389 ds7.ca.sample.com.

Servers behind the domain name
ds1.ca.sample.com
ds2.ca.sample.com
ds3.ca.sample.com
ds4.ca.sample.com
ds5.ca.sample.com
ds6.ca.sample.com
ds7.ca.sample.com







 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment