use OS specific ping (CLI/socket)

This commit is contained in:
wienfuchs 2020-05-25 13:12:41 +02:00
parent 97bbb4f8f7
commit 1a7e30bbf6
No known key found for this signature in database
GPG Key ID: B095DFF49268F245
1 changed files with 58 additions and 32 deletions

View File

@ -180,23 +180,46 @@ class StatusUpdater
$max_runs = 1;
}
$serverIp = $this->server['ip'];
$pingCommand = 'ping6';
$ping_count = " -c ";
$result = null;
// Windows / Linux variant: use socket on Windows, commandline on Linux
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// socket ping - Code from http://stackoverflow.com/a/20467492
// save response time
$starttime = microtime(true);
// set ping payload
$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' => 10, 'usec' => 0));
socket_connect($socket, $serverIp, null);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
$status = true;
} else {
$status = false;
// set error message
$errorcode = socket_last_error();
$this->error = "Couldn't create socket [" . $errorcode . "]: " . socket_strerror($errorcode);
}
$this->rtime = microtime(true) - $starttime;
socket_close($socket);
} else {
// Choose right ping version, ping6 for IPV6, ping for IPV4
$pingCommand = 'ping6';
if (filter_var($serverIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
$pingCommand = 'ping';
}
// Use -n instead of -c for Windows machines
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$ping_count = " -n ";
}
// execute PING
$txt = exec($pingCommand . $ping_count . $max_runs . " " . $serverIp . " 2>&1", $output);
$txt = exec($pingCommand . " -c " . $max_runs . " " . $serverIp . " 2>&1", $output);
// Check if output is PING and if transmitted packets is equal to received packets.
preg_match('/^(\d{1,3}) packets transmitted, (\d{1,3}).*$/', $output[count($output) - 2], $output_package_loss);
@ -223,6 +246,9 @@ class StatusUpdater
$this->error = $output[count($output) - 2];
$status = false;
}
}
// To miliseconds
$this->rtime = $result / 1000;