Skip to content

Instantly share code, notes, and snippets.

@ianchanning
Created June 14, 2013 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianchanning/5783675 to your computer and use it in GitHub Desktop.
Save ianchanning/5783675 to your computer and use it in GitHub Desktop.
PHP Ping Packets
<?php
/**
* Ping a host using an IMCP packet
*
* N.B. This function must be called by an administrator / root user as socket_create requires this
*
* @param string $host domain
* @param integer $timeout seconds
* @return mixed False | Response time milliseconds
*
* @link http://www.php.net/manual/en/function.socket-create.php#101012 Code source
* @author geoff@spacevs.com
*/
function ping ($host, $timeout = 1) {
/* ICMP ping packet with a pre-calculated checksum */
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $host, null);
$ts = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
$result = microtime(true) - $ts;
} else {
$result = false;
}
socket_close($socket);
return $result;
}
?>
@jersonmartinez
Copy link

Hola colega.

Estoy interesado en que el bloque de código que ha compartido me funcione. Lo he probado con una condición de que si retorna algo diferente a false, entonces el ping está correcto. La cuestión es que no me ha funcionado, no retorna algún dato.

¿Le ha funcionado correctamente?

Cuénteme, necesito colaboración. ¡Saludos cordiales!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment