cf ping() function...

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():

<cffunction name="ping" access="public" returntype="struct" output="false">
   <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>
If you do not pass in the target_name (which can be an ip address or website or computer name) it will use 127.0.0.1. It also has the optional 'options' argument. To see it in action, stick the function on a page and run:
<cfdump var="#ping('www.ryanguill.com','-n 10')#" />

Posted on Wed. December 07, 2005 by Ryan Guill
 

1

Does it blink the response 4 times? ;)

Posted on Mon. September 15, 2008 by Jacob Munson #

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.

Posted on Mon. September 15, 2008 by Ryan Guill #

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. :)

Posted on Mon. September 15, 2008 by Jacob Munson #

4

right, this is windows only, and yes, on linux some additional params would definately be required ;)

Posted on Mon. September 15, 2008 by Ryan Guill #

5

Ryan - I got a timeout pinging your server with your code. Why? Pat

Posted on Mon. September 15, 2008 by Patrick Whittingham #

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?

Posted on Mon. September 15, 2008 by Ryan Guill #
Design, Photograph, Work © Ryan Guill, aDeepBlue 2013: All Rights Reserved. | Contact | RSS Feed