Created
June 14, 2013 17:17
-
-
Save ianchanning/5783675 to your computer and use it in GitHub Desktop.
PHP Ping Packets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 [email protected] | |
*/ | |
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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!