我的解决方案是...我使用此功能检查了外部设备上的 ping。
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);
}
// Making the package
$type= "\x08";
$code= "\x00";
$checksum= "\x00\x00";
$identifier = "\x00\x00";
$seqNumber = "\x00\x00";
$data= "Scarface";
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
$checksum = icmpChecksum($package); // Calculate the checksum
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
// And off to the sockets
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option ( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>0) );
socket_connect($socket, 192.168.0.1, null);
$startTime = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (false === (@socket_read($socket, 255))) {
echo "Harddware is OFF--->";
} else {
echo "Harddware is ON--->";
}
如果我使用带有 shell 功能的经典 ping,那么这对我来说完全不稳定 :)