R
If you're embarrassed by the failure of the process, you can use the flag. -c countto limit the number of queries.ping6 -c 1 example.com
On screen, you need to use it. http://php.net/manual/ru/function.escapeshellarg.php Not escapeshellcmd: $cmd = 'ping6 -c1 ' . escapeshellarg($ipv6);
It's not a silver bullet, the function has a lot of low-level issues: Over four years ago https://bugs.php.net/bug.php?id=65755 ♪It doesn't work for Win. https://bugs.php.net/bug.php?id=49446 Lokal processing. https://bugs.php.net/bug.php?id=54391 Alternative silks. https://bugs.php.net/bug.php?id=61706 It is possible to protect itself (no specialist, no guarantee), pre-limiting possible incorrect symbols: if (!preg_match('/\A[-_.@\pN\pL]+\z/ui', $ipv6)) throw new \Exception('Bad domain'); // \pN - числовые символы, \pL - буквенные
Ping can also be performed on its own, through ICMPfunction ping($host, $timeout = 1, $ipv6 = false) {
/* ICMP ping packet with a pre-calculated checksum */
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket = socket_create($ipv6 ? AF_INET6 : 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;
}
Used https://stackoverflow.com/a/16650339/2110496