Wolf Head
Wolf Head

Reputation: 45

Ping an ip using php and result in only the average response time.

Anyone know know of a simple clean way to ping an IP address in php and echo the result of the average ping time only?

For instance I'll get "Minimum = 35ms, Maximum = 35ms, Average = 35ms " when all I really want is "35"

Thanks.

Upvotes: 3

Views: 7413

Answers (3)

Krzysztof Karski
Krzysztof Karski

Reputation: 829

I guess that what you want is this:

const PING_REGEX_TIME = '/time(=|<)(.*)ms/';
const PING_TIMEOUT = 10;
const PING_COUNT = 1;

$os = strtoupper(substr(PHP_OS, 0, 3));
$url = 'www.google.com';

// prepare command
$cmd = sprintf('ping -w %d -%s %d %s',
    PING_TIMEOUT,
    $os === 'WIN' ? 'n' : 'c',
    PING_COUNT,
    escapeshellarg($url)
);

exec($cmd, $output, $result);

if (0 !== $result) {
    // something went wrong
}

$pingResults = preg_grep(PING_REGEX_TIME, $output); // discard output lines we don't need
$pingResult = array_shift($pingResults); // we wanted just one ping anyway

if (!empty($pingResult)) {
    preg_match(PING_REGEX_TIME, $pingResult, $matches); // we get what we want here

    $ping = floatval(trim($matches[2])); // here's our time
} else {
    // something went wrong (mangled output)
}

This is an example of getting just the ms from a single ping, but it's easy to adjust it to get whatever you want. All you have to do is play with the regex, timeout and count constants.

You may also want to perhaps adjust the regex (or add more of them) according to the OS since Linux ping will provide differently formatted results from a Windows one.

Upvotes: 5

Arun Killu
Arun Killu

Reputation: 14233

You can use the exec()-function to execute the shell command ping like in this example:

 <?php
    function GetPing($ip=NULL) {
     if(empty($ip)) {$ip = $_SERVER['REMOTE_ADDR'];}
     if(getenv("OS")=="Windows_NT") {
       $ping=explode(",", $exec);
       return $ping[1];//Maximum = 78ms
     }
     else {
      $exec = exec("ping -c 3 -s 64 -t 64 ".$ip);
      $array = explode("/", end(explode("=", $exec )) );
      return ceil($array[1]) . 'ms';
     }
    }

    echo GetPing();
    ?>

Source: http://php.net/manual/en/function.exec.php

Upvotes: 5

Micah
Micah

Reputation: 96

Found this function online a while back, sorry I don't remember where to credit, but you can use it with a for-loop to get an average:

function ping($host, $timeout = 10) 
{ 
    $output = array(); 
    $com = 'ping -n -w ' . $timeout . ' -c 1 ' . escapeshellarg($host); 
    $exitcode = 0; 
    exec($com, $output, $exitcode); 
    if ($exitcode == 0 || $exitcode == 1) 
    {
        foreach($output as $cline) 
        { 
            if (strpos($cline, ' bytes from ') !== FALSE) 
            { 
                $out = (int)ceil(floatval(substr($cline, strpos($cline, 'time=') + 5))); 
                return $out; 
            } 
        } 
    } 

    return FALSE; 
}

$total = 0;
for ($i = 0; $i<=9; $i++)
{
        $total += ping('www.google.com');
}

echo $total/10;

Just change out the number of times in the for loop as appropriate..

Upvotes: 4

Related Questions