Dec 282010

I have found being able to ping from a webpage useful on occasion so I thought I would post up a few snippets that I used to ping computers.

var oLoc = new ActiveXObject('WbemScripting.SWbemLocator');
var oSrv = oLoc.ConnectServer(null,'/root/cimv2');
var PingServer = new Enumerator(oSrv.ExecQuery('SELECT * FROM Win32_PingStatus WHERE Address = "' + Computer + '" AND Timeout = 5000'));
var respcode = PingServer.item().StatusCode;
if(respcode == 0) {
alert("Reply from "+Computer)
} else {
alert("Error pinging "+Computer+"\nError Code : "+respcode);

That’s pretty much a cut down version of what I use, it’s missing all the HTML obviously but that’s the core of it.
The “Computer” variable is the address or hostname that you want to ping, and “Timeout” is the maximum time before the ping times out in milliseconds.

Now a quick explanation :
First couple of lines creates the ActiveX Object and starts a connection using WMI
Third line creates the Enumerator object that contains the results from the query that returns the ping results.
Fourth and Fifth lines get to the results and returns the Status Code respectively.
From there, a response of 0 means the ping was good, otherwise anything else means it was bad.
This link has the list of status codes and their meanings.

I have used this at work to see if computers are online quickly as I have this and a few other useful scripts running in a little window off to the side.


 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>