Pete Freitag Pete Freitag

DNS Query with ColdFusion

Published on October 27, 2005
By Pete Freitag
coldfusionjava

It has been a while since I've posted one of these java + cfml tricks, so here's a neat one for ya'.

You can use the Java Naming and Directory Interface (JNDI) to perform a DNS query in ColdFusion.

In the example below I use JNDI to look up the MX records (mail exchangers) for gmail.com:

<cfset env = CreateObject("java", "java.util.Hashtable")>
<cfset env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory")>
<cfset env.put("java.naming.provider.url", "dns://10.0.0.65")>
<cfset env.put("com.sun.jndi.dns.timeout.initial", "2000")>
<cfset env.put("com.sun.jndi.dns.timeout.retries", "3")>
<cfset dirContext = CreateObject("java", "javax.naming.directory.InitialDirContext")>
<cfset dirContext.init(env)>
<cfset type = ArrayNew(1)>
<cfset type[1] = "MX">
<cfset attributes = dirContext.getAttributes("gmail.com", type)>
<cfset atribEnum = attributes.getAll()>
<cfloop condition="#atribEnum.hasMore()#">
<cfset attribute = atribEnum.next()>
<cfoutput>#attribute.toString()#</cfoutput><br />
</cfloop>

You will want to point the java.naming.provider.url to the IP address of a dns server, in my case I use: dns://10.0.0.65.

You can also make different types of queries such as A (address) or TXT (text records).

I have written a commercial DNS component for ColdFusion as well.



coldfusion cfml dns tricks java jndi

DNS Query with ColdFusion was first published on October 27, 2005.

If you like reading about coldfusion, cfml, dns, tricks, java, or jndi then you might also like:

Fixinator

The Fixinator Code Security Scanner for ColdFusion & CFML is an easy to use security tool that every CF developer can use. It can also easily integrate into CI for automatic scanning on every commit.


Try Fixinator

CFBreak
The weekly newsletter for the CFML Community


Comments

i think you can just use a struct "instead" of a hastable.
by PaulH on 10/27/2005 at 11:05:33 PM UTC
Holy cow, thank you so much. I've been trying to figure out how to do this in ColdFusion for so long. Thank you!
by John King on 12/15/2009 at 11:57:31 PM UTC