Autor: Antonio Leal Elizondo 12/Ago/03
e-mail: invasor@hotmail.com
This script pings a machine and checks if it's alive or not.
It can run a command in case the machine is not answering
<?php
// Variables
//-------------------------
$unix = 1; //Put 1 here if you are using a UNIX machine
$windows = 0; //Put 1 here if you are using a Windown machine
$unix = (bool) $unix;
$win = (bool) $windows;
$count= 3; // Number of times to ping
$host = "177.17.31.165"; // IP of the remote server
$find = "Tiempo de espera agotado"; // String to look for
$shell= "pwd"; // Command to execute if the machine does not answer.
$findUnix="0 packets received";
//Check the IP
if ($unix==1){
$comm="ping -t $count$host";
$res=shell_exec($comm);
}else{
$comm="ping -n $count$host";
$res=shell_exec($comm);
}
// system("killall ping");// Kill all pings in case they exist.
//echo $res;
$flag= strstr($res,$findUnix);
$long=strlen($flag);
if ($long<>0){
echo "\n An error has occured at $host\n ";
echo date("D F Y h:i a");
echo "\n Executng auxiliary command \n ";
$res=shell_exec($shell);
echo $res;
}else{
echo "\n There are no problems with $host \n";
echo date("D F Y h:i a"). "\n" ;
}
?>