DNS Query with ColdFusion

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.
Related Entries
- Serializing CFC's in ColdFusion 8 - August 6, 2007
- Reverse IP Address Lookup with ColdFusion + Java - January 10, 2007
- Null Java References in CF 6 vs 7 - January 10, 2006
- What Version of Java is ColdFusion Using? - February 25, 2010
- Hands on ColdFusion Security Training - February 4, 2010
Trackbacks
Trackback Address: 487/1969C68F92EE363C7C09A94AAB1E88B1
Comments
On 10/27/2005 at 11:05:33 PM EDT PaulH wrote:
1
i think you can just use a struct "instead" of a hastable.
On 10/29/2005 at 3:44:25 PM EDT Daniel Plesse wrote:
2
Since your the java guru, here's my little program. I was wondering if you could get it to work better?
<cfif NOT isdefined("session.HSQLDB2")> Do once <BR>
<cfscript> remoteClassLoader = createObject("component","RemoteClassLoader"); urlArray = arrayNew(1); urlArray[1] = "http://www.geocities.com/empiricallyspeaking/classes/hsqldb.jar"; urlArray[2] = "http://www.geocities.com/empiricallyspeaking/classes/testHSQLDB2.jar"; remoteClassLoader.setRemoteClassPaths(urlArray); session.HSQLDB2 = remoteClassLoader.getRemoteClass("HSQLDB2").newInstance(); </cfscript>
<cftry> <cfset session.holdtheconnection = session.HSQLDB2.HSQLDB_getconnection()> <cfcatch type="any"> <cfdump var="#cfcatch#"> </cfcatch> </cftry>
</cfif>
<--- session.holdtheconnection.isClosed() is YES why is that and how can I keep it open. ---->
On 03/06/2008 at 9:05:58 AM EST Randy wrote:
3
I added the following:
<cfset type = ArrayNew(1)> <cfset type[1] = "A"> <cfset type[2] = "AAAA"> <cfset type[3] = "CNAME"> <cfset type[4] = "MX"> <cfset type[5] = "NAPTR"> <cfset type[6] = "NS"> <cfset type[7] = "PTR"> <cfset type[8] = "SOA"> <cfset type[9] = "SRV"> <cfset type[10] = "TXT">
and it only pulls type[6]
I can switch out the different types but it only shows 1.
-Randy
On 12/15/2009 at 11:57:31 PM EST John King wrote:
4
Holy cow, thank you so much. I've been trying to figure out how to do this in ColdFusion for so long. Thank you!
Post a Comment
Recent Entries
- Cache Template in Request Setting Explained
- What Version of Java is ColdFusion Using?
- ColdFusion 9 Performance Brief from Adobe
- Request Filtering in IIS 7 Howto
- J2EE Session Cookies on ColdFusion / JRun
- Hands on ColdFusion Security Training
- ColdFusion 9 Solr Vulnerability - Are you at Risk?
- FCKEditor Year 2010 Bug for Firefox 3.6 with ColdFusion
<cfif NOT isdefined("session.HSQLDB2")> Do once <BR>
<cfscript> remoteClassLoader = createObject("component","RemoteClassLoader"); urlArray = arrayNew(1); urlArray[1] = "http://www.geocities.com/empiricallyspeaking/classes/hsqldb.jar"; urlArray[2] = "http://www.geocities.com/empiricallyspeaking/classes/testHSQLDB2.jar"; remoteClassLoader.setRemoteClassPaths(urlArray); session.HSQLDB2 = remoteClassLoader.getRemoteClass("HSQLDB2").newInstance(); </cfscript>
<cftry> <cfset session.holdtheconnection = session.HSQLDB2.HSQLDB_getconnection()> <cfcatch type="any"> <cfdump var="#cfcatch#"> </cfcatch> </cftry>
</cfif>
<--- session.holdtheconnection.isClosed() is YES why is that and how can I keep it open. ---->
<cfset type = ArrayNew(1)> <cfset type[1] = "A"> <cfset type[2] = "AAAA"> <cfset type[3] = "CNAME"> <cfset type[4] = "MX"> <cfset type[5] = "NAPTR"> <cfset type[6] = "NS"> <cfset type[7] = "PTR"> <cfset type[8] = "SOA"> <cfset type[9] = "SRV"> <cfset type[10] = "TXT">
and it only pulls type[6]
I can switch out the different types but it only shows 1.
-Randy



add to del.icio.us



