Yo uso esto... a partir de ahí, puedes procesar el resultado como desees.
Un saludo.
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="es-MX">
<head>
<title>Ping to Planet with 32 data bytes</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<STYLE type=text/css>
BODY { FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Verdana,arial, helvetica, serif; margin : 25 25 25 25;}
</STYLE>
</head>
<BODY bgcolor="#D5D5D5" text="#000000" id="all" leftmargin="25" topmargin="25" marginwidth="25" marginheight="25" link="#000020" vlink="#000020" alink="#000020">
<?php
/*
// http://localhost/ping.php
*/
//---------------------------------------------------------------------------------------------------
// definiciones
error_reporting( E_ALL );
//---------------------------------------------------------------------------------------------------
// funciones
// Checksum calculation function
function icmpChecksum($data) {
if (strlen($data)%2)
$data .= "\x00";
$bit = unpack('n*', $data);
$sum = array_sum($bit);
while ($sum >> 16)
$sum = ($sum >> 16) + ($sum & 0xffff);
return pack('n*', ~$sum);
}
//---------------------------------------------------------------------------------------------------
// main
echo "<center><b>Ping to 'XxXxXx' with 32 data bytes</b><br>";
echo "</center><br>";
echo "<br><pre><br>";
// Making the package
$type= "\x08"; // Echo Request
$code= "\x00";
$checksum= "\x00\x00";
$identifier = "\x00\x00";
$seqNumber = "\x00\x00";
$data= md5(microtime(true)); // 32 bytes de datos
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
$checksum = icmpChecksum($package); // Calculate the checksum
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
$timeout = 15; //timeout in seconds
// And off to the sockets
$socket = socket_create( AF_INET, SOCK_RAW, 1 )
or die("Unable to create socket\n");
socket_clear_error();
$time = time();
while ( !@socket_connect($socket, "www.XxXxXx.com", null )) {
if ((time() - $time) >= $timeout) {
socket_close($socket);
die("Connection timed out.\n");
}
$err = socket_last_error($socket);
if ($err == 115 || $err == 114) {
if ((time() - $time) >= $timeout) {
socket_close($socket);
die("Connection timed out.\n");
}
sleep(1);
continue;
}
die(socket_strerror($err) . "\n");
}
$startTime = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
$stopTime = microtime(true);
$pingTime = $stopTime - $startTime;
if( $pingTime < 0 ) {
$stopTime += 1.0;
$pingTime = $stopTime - $startTime;
}
$pingTime = bcsub( $pingTime, 0, 3 );
echo "$pingTime seconds<br>";
}
socket_close($socket);
echo "<br>";
echo "</pre><hr><br>";
echo "<center><small>";
echo date("l dS of F Y - h:i:s A") . "<br>";
echo "</small></center>";
echo "</body>";
echo "</html>";
//---------------------------------------------------------------------------------------------------
// END
?>