Mar 012013
 

One of my headless wireless devices have been freezing lately, and since it’s in a somewhat isolated location, I have no idea when it freezes.
So I wrote up a quick script to check to see whether it’s up and I’m running it every hour so that it alerts me when it goes down. It will also alert me once it comes back up, in case it comes back up by itself !

The script will also save the last state that the device was in so that it will only email me once when the device goes down.


#!/bin/bash
email=YOUR@EMAIL.HERE

ip=$1
if [[ -z $1 ]]; then
echo Usage: ./pingcheck.sh \
exit
fi
if [ ! -f /tmp/$1.status ]; then
touch /tmp/$1.status
fi

oldstatus=`cat /tmp/$1.status`
reply=`ping -c 1 $ip | grep 64\ bytes | wc -l`
echo $reply > /tmp/$1.status

if [[ $oldstatus -ne $reply ]]; then
if [[ $reply -eq 1 ]]; then
echo $1 is online | mail -s "Node is online" $email
else
echo $1 is offline | mail -s "Node is offline" $email
fi
fi

Share
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'));
PingServer.moveFirst();
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.

Share