<?php
function _pavg()
{
  global 
$i$c$t;

  
$avg sprintf('%.3f',$t/$c);
  
$loss sprintf('%d',($i-$c)*100/$i);

  echo 
"$i packets transmitted, $c received, ${loss}% packet loss, rtt avg = $avg ms\n";
  exit(
0);
}

require_once 
'class.icmp.php';

set_time_limit(0);

if(
preg_match('/WIN/i',PHP_OS)) error_reporting(E_ALL E_NOTICE);
else
{
  declare(
ticks 1);
  
## 2 3 5 15 19 --> kill, 1 --> HUP
  ##
  
pcntl_signal(SIGHUP,'_pavg'); // 1
  
pcntl_signal(SIGINT,'_pavg'); // 2, Ctrl+C
  
pcntl_signal(SIGQUIT,'_pavg'); // 3
  
pcntl_signal(SIGABRT,'_pavg'); // 6
  //pcntl_signal(SIGKILL,'_pavg'); // 9, it's fault
  //pcntl_signal(SIGUSR1,'_pavg'); // 10, does not work
  
pcntl_signal(SIGALRM,'_pavg'); // 14
  
pcntl_signal(SIGTERM,'_pavg'); // 15
  //pcntl_signal(SIGSTOP,'_pavg'); // 19, it's fault
}

if(!
$ip $_SERVER['argv'][1]) { echo 'type ip.address'; exit(1); }
if(
preg_match('/[a-z]/',$ip)) $ip gethostbyname($ip);

$i $c 1;
$t 0;
while(
TRUE)
{
  list(
$ms$error) = icmp::ping($ip);
  if(
$ms >= 0)
  {
    
$msg $ms.' ms';
    
$t += $ms;
    
$c++;
  }
  else 
$msg $error;

  echo 
"$ip seq:$i $msg\n";
  
$i++;
  
sleep(1);
}

exit(
0);
?>