Thu 19 Feb 2009
Coldfusion: DNS Reverse Lookup
Posted by JC under ColdFusion
[3] Comments
I have no idea why this information was so difficult to find, but I'm posting it here in case I ever need it again.
To do a reverse lookup in coldfusion:
<cfscript>
rawIP = createObject("java", "java.net.InetAddress").getByName('#ipaddress#').getAddress();
hostname = createObject("java", "java.net.InetAddress").getByAddress('#rawIP#').getHostName();
chostname = createObject("java", "java.net.InetAddress").getByAddress('#rawIP#').getCanonicalHostName();
</cfscript>
Note — this is slow. If you're doing them in bulk, you may need to go in small batches or set a higher timeout.
Also, this was perhaps useful: http://api.hostip.info/get_html.php?ip=#ipaddress#&position=true — returns the country, city, state, and geotag info for an IP address, where available.
This works great if it wasnt so slow. We're using this to log the host names of our users, and Im looking up anywhere between 6-10k addresses at a time, which doesnt make this usage feasible. What we've noticed though, is that it only takes a while if it is unable to find a host name. Otherwise, its back in less than a second. Do you or anyone know of any way to cancel the request if it takes longer than a specified period of time and just move on to the next one if that's the case?
Hmm.. You could try wrapping it in cflock with a short timeout. That might work.
take a look at this blog (not mine) – reverseDNS lookup without locking up using dnsjava.org:
http://www.phillnacelli.net/blog/index.cfm/2007/2/21/DNS-Lookup-in-ColdFusion
Works for me…