I had a request today to be able to ping a server or website using coldfusion and extract the importaint information. So I whipped up a small function that allows you to do just that. This function returns a struct full of information, you can decide what you need out of it. It *could* return a little more information, but that is left as an exercise to the reader. Also, I have done the same thing for tracert, and the windows systeminfo command (if you dont know what that is, go to a command line and type systeminfo), but I haven't packaged them up into functions yet. Stay tuned for those. This code has been tested on cfmx 7.0.0, and win2k3 server. Im not sure if this would work on linux or osx or any other system because im not sure how their ping commands work. I have a feeling this wouldn't work on those systems. So without further ado, here is ping():
<cfargument name="target_name" type="string" required="false" default="127.0.0.1" />
<cfargument name="options" type="string" required="False" default="" />
<cfset VAR out = structNew() />
<cfset VAR execRet = "" />
<cfset VAR i = 0 />
<cftry>
<cfexecute name="ping.exe" timeout="60" arguments="#arguments.target_name# #arguments.options#" variable="execRet" />
<cfcatch>
<cfthrow message="ping process timed out" />
</cfcatch>
</cftry>
<cfset out.target_name = arguments.target_name />
<cfset out.options = arguments.options />
<cfset out.execResults = execRet />
<cfset out.arExecResults = listToArray(execRet, createObject("java", "java.lang.System").getProperty("line.separator"))/>
<cfset out.summary = out.arExecResults[arrayLen(out.arExecResults)] />
<cfset out.arSummary = listToArray(out.summary) />
<cfset out.summaryInfo = structNew() />
<cfloop from="1" to="#arrayLen(out.arSummary)#" index="i">
<cfset out.summaryInfo[trim(listFirst(out.arSummary[i],"="))] = trim(listLast(out.arSummary[i],"=")) />
</cfloop>
<cfreturn out />
</cffunction>

1
Does it blink the response 4 times? ;)
2
no, unfortunately the cfexecute call does not give you feedback progressively, so you have to wait until the entire call is finished before you get any output.
3
I was just joking, but you got me thinking. This will be Windows only, because you are calling ping.exe. Linux/Mac has a similar ping command though, it shouldn't be hard to port it. If you do try it though, remember that on Linux, by default the ping command is indefinite. You have to give it a parameter to tell it how many responses you want, or it will go forever. That could cause some problems, I'd assume. :)
4
right, this is windows only, and yes, on linux some additional params would definately be required ;)
5
Ryan - I got a timeout pinging your server with your code. Why? Pat
6
Patrick, Im sorry, my email server is not working. I am not sure why your ping doesn't work, can you ping me from a command line?