#!/usr/bin/env php $args The arguments. * * @return string */ function ss_ping_run(string $name, array $args) : string { // ping -c 1 waits up to 10 seconds for a reply, so allow a little more $output = poller_script_exec($name, $args, 12); if ($output === null) { return ''; } return $output['stdout'] . $output['stderr']; } /** * Picks the first line of ping output that carries a reply or a name error. * * @param string $output The ping output. * @param bool $include_errors Whether resolver errors also match. * * @return string The line, or an empty string. */ function ss_ping_line(string $output, bool $include_errors) : string { $pattern = '(?:icmp_)?[sr]eq=.*t(ime|emps)'; if ($include_errors) { $pattern .= '|unknown host|Unknown host|not supported|Name or service not known|No address associated with name'; } foreach (explode("\n", $output) as $line) { if (preg_match('/(' . $pattern . ')/', $line)) { return $line; } } return ''; } /** * Extracts the round trip time from a ping reply line, converting usec to ms. * * @param string $line A reply line such as 'icmp_seq=1 ttl=64 time=0.045 ms'. * * @return string The time in ms, or U. */ function ss_ping_time(string $line) : string { if (!preg_match('/(.*t(?:ime|emps)=)([\w\.]*).*(ms|usec)/', $line, $matches)) { return 'U'; } if ($matches[2] === '') { return 'U'; } if ($matches[3] === 'usec') { // Perl prints numbers with 15 significant digits return sprintf('%.15g', (float) $matches[2] / 1000); } return $matches[2]; }