Re: Active Directory DNS query
- From: shanevanle@xxxxxxxxx
- Date: 22 Dec 2005 13:00:30 -0800
Here is the sample code I have been given. I need to rewrite this in
VB as it is currently in java. I don't exactly understand what the
code is. The standards in our IS department require us to use the
method of querying
for AD servers. If anyone can explain to me what is going on in the
code and maybe some samples I can use to do this in VB, it would be
greatly appreciated. Someone has said something about how ADSI in VB
does this automatically. I am not sure what that means or what ADSI
is, so if you have an example of that, than that would be appreciated.
//////////////////////////////////////////////////////////////////////////////////////
//
// This is an example of how DNS can be queried to find Active
Directory
//
//
// The results of this query should be sorted by the DNS records
weight,
// using the lowest weighted records first and the higher weighted
records
// last.
//
// To find Domain Controllers in a specific Active Directory site
// look for the following DNS SRV record format:
// Format: _ldap._tcp.<SITE NAME>.dc._msdcs.<DOMAIN NAME>.domain.com
//
// To find Global Catalogs in a specific Active Directory site
// look for the following DNS SRV record format:
// Format: _ldap._tcp.<SITE NAME>._sites.gc._msdcs.domain.com
//
///////////////////////////////////////////////////////////////////////////////////////
import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;
public class findad
{
public static void main( String args[] )
{
String domainController = null;
String dnsServiceName = "_ldap._tcp";
//Specifies a Global Catalog server
String aDSite = "<site>";
//The Active Directory SITE to look
String dnsDomain = "us.domain.com";
//The DNS base Zone to look
String aDDomain = aDSite +
"._sites.dc._msdcs." +
dnsDomain; //The DNS Zone to look
String dnsNameServer = "ad-dns.domain.com";
//The DNS server to query
String dnsQueryType[] = {"SRV"};
//DNS record type to query
String retDomainController = null;
//Returned Domain Controller
Long retDNSweight;
//Returned DNS record weight for the returned Domain Controller
try {
Hashtable dnsEnv = new Hashtable();
dnsEnv.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
dnsEnv.put("java.naming.provider.url", "dns://"
+ dnsNameServer +
"/" + aDDomain);
DirContext dnsContext = new
InitialDirContext(dnsEnv);
Attributes dnsQueryResult =
dnsContext.getAttributes(dnsServiceName,
dnsQueryType);
if (dnsQueryResult == null)
{
System.out.println("Returned NULL");
}
for (NamingEnumeration dnsRR =
dnsQueryResult.getAll();dnsRR.hasMoreElements();)
{
Attribute rr = (Attribute)dnsRR.next();
String attrId = rr.getID();
for (Enumeration vals =
rr.getAll();vals.hasMoreElements();)
{
domainController =
vals.nextElement().toString();
StringTokenizer st = new
StringTokenizer( domainController );
retDNSweight = new
Long(st.nextToken());
st.nextToken();
st.nextToken();
retDomainController =
st.nextToken();
System.out.println("Domain
Controller: " + retDomainController + "
Weight: " + retDNSweight.longValue());
retDomainController = null;
retDNSweight = null;
}
}
dnsContext.close();
} catch(Exception e) {
System.err.println("Error performing DNS lookup
for: " + aDDomain +
"\n" + e);
}
}
.
- References:
- Active Directory DNS query
- From: shanevanle
- Active Directory DNS query
- Prev by Date: Re: VU Meter in VB6 (No... Couldn't get waveout meter Error !)
- Next by Date: Re: ERROR
- Previous by thread: Active Directory DNS query
- Next by thread: Re: Active Directory DNS query
- Index(es):
Relevant Pages
|