Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created September 13, 2021 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/a85a907f78712d5241d52de6c3f12ec5 to your computer and use it in GitHub Desktop.
Save kobus1998/a85a907f78712d5241d52de6c3f12ec5 to your computer and use it in GitHub Desktop.
average ping from server
#!/usr/local/bin/php -q
<?php
// php ping.php google.com
$sHost = escapeshellarg($argv[1] ?? 'localhost');
$sCmd = "ping {$sHost}";
echo $sCmd . "\n";
$proc = popen($sCmd, 'r');
$aNums = [];
while (!feof($proc)) {
$sLine = fread($proc, 4096);
$aParams = array_filter(explode(" ", $sLine), function ($sParams) {
return strpos($sParams, '=') !== false || !strpos($sParams, 'time');
});
$aNums[] = str_replace('time=', '', reset($aParams));
echo round(array_sum($aNums) / count($aNums), 8) . " Ping\n";
flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment