Source for file Ping.php
Documentation is available at Ping.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Martin Jansen <mj@php.net> |
// | Tomas V.V.Cox <cox@idecnet.com> |
// | Jan Lehnardt <jan@php.net> |
// | Kai Schr�der <k.schroeder@php.net> |
// +----------------------------------------------------------------------+
// $Id: Ping.php,v 1.45 2007/06/21 21:09:44 cconstantine Exp $
require_once "OS/Guess.php";
define('NET_PING_FAILED_MSG', 'execution of ping failed' );
define('NET_PING_HOST_NOT_FOUND_MSG', 'unknown host' );
define('NET_PING_INVALID_ARGUMENTS_MSG', 'invalid argument array' );
define('NET_PING_CANT_LOCATE_PING_BINARY_MSG', 'unable to locate the ping binary');
define('NET_PING_RESULT_UNSUPPORTED_BACKEND_MSG', 'Backend not Supported' );
define('NET_PING_HOST_NOT_FOUND', 1 );
define('NET_PING_INVALID_ARGUMENTS', 2 );
define('NET_PING_CANT_LOCATE_PING_BINARY', 3 );
define('NET_PING_RESULT_UNSUPPORTED_BACKEND', 4 );
* - add Net_Ping_Result parser for:
* - fix Net_Ping::checkHost()
* Wrapper class for ping calls
* require_once "Net/Ping.php";
* $ping = Net_Ping::factory();
* if(PEAR::isError($ping)) {
* echo $ping->getMessage();
* $ping->setArgs(array('count' => 2));
* var_dump($ping->ping('example.com'));
* @author Jan Lehnardt <jan@php.net>
* @version $Revision: 1.45 $
* Location where the ping program is stored
* Array with the result from the ping execution
* OS_Guess->getSysname result
* Indicates if an empty array was given to setArgs
* Contains the argument->option relation
var $_argRelation = array ();
* Constructor for the Class
function Net_Ping ($ping_path, $sysname)
$this->_ping_path = $ping_path;
$this->_sysname = $sysname;
$this->_initArgRelation ();
} /* function Net_Ping() */
return new Net_Ping($ping_path, $sysname);
} /* function factory() */
* Resolve the system name
function _setSystemName ()
$OS_Guess = new OS_Guess;
$sysname = $OS_Guess->getSysname ();
// Refine the sysname for different Linux bundles/vendors. (This
// should go away if OS_Guess was ever extended to give vendor
// and vendor-version guesses.)
// Bear in mind that $sysname is eventually used to craft a
// method name to figure out which backend gets used to parse
// the ping output. Elsewhere, we'll set $sysname back before
if ('linux' == $sysname) {
$sysname = 'linuxdebian';
$sysname = 'linuxredhat8';
$sysname = 'linuxredhat9';
} /* function _setSystemName */
* Set the arguments array
* @param array $args Hash with options
* @return mixed true or PEAR_error
$this->_setNoArgs ($args);
} /* function setArgs() */
* @param array $args Hash with options
function _setNoArgs ($args)
} /* function _setNoArgs() */
* Sets the system's path to the ping binary
function _setPingPath ($sysname)
if ("windows" == $sysname) {
$ping_path = exec("which ping", $output, $status);
} /* function _setPingPath() */
* Creates the argument list according to platform differences
* @return string Argument line
function _createArgList ()
foreach($this->_args AS $option => $value) {
if(!empty ($option) && isset ($this->_argRelation[$this->_sysname][$option]) && NULL != $this->_argRelation[$this->_sysname][$option]) {
$ {$option} = $this->_argRelation[$this->_sysname][$option]. " ". $value. " ";
switch($this->_sysname) {
if ($size || $count || $iface) {
/* $size and $count must be _both_ defined */
$retval['pre'] = $iface. $seq. $ttl;
$retval['post'] = $size. $count;
$retval['pre'] = $quiet. $count. $ttl. $timeout;
$retval['pre'] = $count. $timeout. $size;
$retval['pre'] = $quiet. $count. $iface. $size. $ttl. $timeout;
$retval['pre'] = $quiet. $count. $iface. $size. $ttl. $timeout;
$retval['pre'] = $quiet. $deadline. $count. $ttl. $size. $timeout;
$retval['pre'] = $quiet. $count. $ttl. $size. $timeout;
$this->_sysname = 'linux'; // undo linux vendor refinement hack
$retval['pre'] = $iface. $ttl. $count. $quiet. $size. $deadline;
$this->_sysname = 'linux'; // undo linux vendor refinement hack
$retval['pre'] = $timeout. $iface. $ttl. $count. $quiet. $size. $deadline;
$this->_sysname = 'linux'; // undo linux vendor refinement hack
$retval['pre'] = $count. $ttl. $timeout;
$retval['post'] = $size. $count;
$retval['pre'] = $count. $timeout. $ttl. $size;
} /* function _createArgList() */
* @param string $host hostname
* @return mixed String on error or array with the result
$this->setArgs(array ('count' => 3 ));
$argList = $this->_createArgList ();
$cmd = $this->_ping_path. " ". $argList['pre']. " ". $host. " ". $argList['post'];
// since we return a new instance of Net_Ping_Result (on
// success), users may call the ping() method repeatedly to
// perform unrelated ping tests Make sure we don't have raw data
// from a previous call laying in the _result array.
$this->_result = array ();
exec($cmd, $this->_result);
if (count($this->_result) == 0 ) {
return Net_Ping_Result ::factory ($this->_result, $this->_sysname);
* Check if a host is up by pinging it
* @param string $host The host to test
* @param bool $severely If some of the packages did reach the host
* and severely is false the function will return true
* @return bool True on success or false otherwise
$this->setArgs(array ("count" => 10 ,
$res = $this->ping($host);
if (PEAR ::isError ($res)) {
trigger_error("Output format seems not to be supported, please report ".
"the following to pear-dev@lists.php.net, including your ".
" version of ping:\n $rep" );
if ($matches[0 ][1 ] == 0 ) {
// [0] => transmitted, [1] => received
if ($matches[0 ][0 ] != $matches[0 ][1 ] && $severely) {
} /* function checkHost() */
* Output errors with PHP trigger_error(). You can silence the errors
* with prefixing a "@" sign to the function call: @Net_Ping::ping(..);
* @param mixed $error a PEAR error or a string with the error message
* @author Kai Schr�der <k.schroeder@php.net>
function _raiseError ($error)
if (PEAR ::isError ($error)) {
$error = $error->getMessage ();
} /* function _raiseError() */
* Creates the argument list according to platform differences
* @return string Argument line
function _initArgRelation ()
$this->_argRelation["sunos"] = array (
$this->_argRelation["freebsd"] = array (
$this->_argRelation["netbsd"] = array (
$this->_argRelation["openbsd"] = array (
$this->_argRelation["darwin"] = array (
$this->_argRelation["linux"] = array (
$this->_argRelation["linuxdebian"] = array (
$this->_argRelation["linuxredhat8"] = array (
$this->_argRelation["linuxredhat9"] = array (
$this->_argRelation["windows"] = array (
$this->_argRelation["hpux"] = array (
$this->_argRelation["aix"] = array (
} /* function _initArgRelation() */
* Container class for Net_Ping results
* @author Jan Lehnardt <jan@php.net>
* @version $Revision: 1.45 $
* ICMP sequence number and associated time in ms
var $_icmp_sequence = array (); /* array($sequence_number => $time ) */
* The target's IP Address
* Number of bytes that are sent with each ICMP request
* The total number of bytes that are sent with all ICMP requests
* The raw Net_Ping::result
var $_raw_data = array ();
* Statistical information about the ping
var $_round_trip = array (); /* array('min' => xxx, 'avg' => yyy, 'max' => zzz) */
* Constructor for the Class
function Net_Ping_Result ($result, $sysname)
$this->_raw_data = $result;
$this->_sysname = $sysname;
} /* function Net_Ping_Result() */
* Factory for Net_Ping_Result
* @param array $result Net_Ping result
* @param string $sysname OS_Guess::sysname
function factory ($result, $sysname)
if (!Net_Ping_Result ::_prepareParseResult ($sysname)) {
return new Net_Ping_Result ($result, $sysname);
} /* function factory() */
* Preparation method for _parseResult
* @param string $sysname OS_Guess::sysname
function _prepareParseResult ($sysname)
return in_array('_parseresult'. $sysname, $parse_methods);
} /* function _prepareParseResult() */
* Delegates the parsing routine according to $this->_sysname
} /* function _parseResult() */
* Parses the output of Linux' ping command
function _parseResultlinux ()
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 4;
/* loop from second elment to the fifths last */
for($idx = 1; $idx < $icmp_seq_count; $idx++ ) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_bytes_per_request = $parts[0 ];
$this->_bytes_total = (int) $parts[0 ] * $icmp_seq_count;
$this->_target_ip = substr($parts[3 ], 0 , -1 );
$stats = explode(',', $this->_raw_data[$raw_data_len - 2 ]);
$transmitted = explode(' ', $stats[0 ]);
$this->_transmitted = $transmitted[0 ];
$received = explode(' ', $stats[1 ]);
$this->_received = $received[1 ];
$this->_loss = (int) $loss[1 ];
/* if mdev field exists, shift input one unit left */
if (false !== strpos($this->_raw_data[$raw_data_len - 1 ], 'mdev')) {
/* do not forget the rtt field */
$this->_round_trip['min'] = ltrim($round_trip[5 ]);
$this->_round_trip['avg'] = $round_trip[6 ];
$this->_round_trip['max'] = $round_trip[7 ];
$this->_round_trip['min'] = ltrim($round_trip[4 ]);
$this->_round_trip['avg'] = $round_trip[5 ];
$this->_round_trip['max'] = $round_trip[6 ];
} /* function _parseResultlinux() */
* Parses the output of NetBSD's ping command
* @see _parseResultfreebsd
function _parseResultnetbsd ()
$this->_parseResultfreebsd ();
} /* function _parseResultnetbsd() */
* Parses the output of Darwin's ping command
function _parseResultdarwin ()
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 5;
/* loop from second elment to the fifths last */
for($idx = 1; $idx < $icmp_seq_count; $idx++ ) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_bytes_per_request = (int) $parts[0 ];
$this->_bytes_total = (int) ($this->_bytes_per_request * $icmp_seq_count);
$this->_target_ip = substr($parts[3 ], 0 , -1 );
$stats = explode(',', $this->_raw_data[$raw_data_len - 2 ]);
$transmitted = explode(' ', $stats[0 ]);
$this->_transmitted = (int) $transmitted[0 ];
$received = explode(' ', $stats[1 ]);
$this->_received = (int) $received[1 ];
$this->_loss = (int) $loss[1 ];
$this->_round_trip['min'] = (float) ltrim($round_trip[3 ]);
$this->_round_trip['avg'] = (float) $round_trip[4 ];
$this->_round_trip['max'] = (float) $round_trip[5 ];
$this->_round_trip['stddev'] = NULL; /* no stddev */
} /* function _parseResultdarwin() */
* Parses the output of HP-UX' ping command
function _parseResulthpux ()
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 5;
/* loop from second elment to the fifths last */
for($idx = 1; $idx <= $icmp_seq_count; $idx++ ) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_bytes_per_request = (int) $parts[0 ];
$this->_bytes_total = (int) ($parts[0 ] * $icmp_seq_count);
$this->_target_ip = NULL; /* no target ip */
$this->_ttl = NULL; /* no ttl */
$stats = explode(',', $this->_raw_data[$raw_data_len - 2 ]);
$transmitted = explode(' ', $stats[0 ]);
$this->_transmitted = (int) $transmitted[0 ];
$received = explode(' ', $stats[1 ]);
$this->_received = (int) $received[1 ];
$this->_loss = (int) $loss[1 ];
$this->_round_trip['min'] = (int) ltrim($round_trip[3 ]);
$this->_round_trip['avg'] = (int) $round_trip[4 ];
$this->_round_trip['max'] = (int) $round_trip[5 ];
$this->_round_trip['stddev'] = NULL; /* no stddev */
} /* function _parseResulthpux() */
* Parses the output of AIX' ping command
function _parseResultaix ()
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 5;
/* loop from second elment to the fifths last */
for($idx = 1; $idx <= $icmp_seq_count; $idx++ ) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_bytes_per_request = (int) $parts[0 ];
$this->_bytes_total = (int) ($parts[0 ] * $icmp_seq_count);
$this->_target_ip = substr($parts[3 ], 0 , -1 );
$stats = explode(',', $this->_raw_data[$raw_data_len - 2 ]);
$transmitted = explode(' ', $stats[0 ]);
$this->_transmitted = (int) $transmitted[0 ];
$received = explode(' ', $stats[1 ]);
$this->_received = (int) $received[1 ];
$this->_loss = (int) $loss[1 ];
$this->_round_trip['min'] = (int) ltrim($round_trip[3 ]);
$this->_round_trip['avg'] = (int) $round_trip[4 ];
$this->_round_trip['max'] = (int) $round_trip[5 ];
$this->_round_trip['stddev'] = NULL; /* no stddev */
} /* function _parseResultaix() */
* Parses the output of FreeBSD's ping command
function _parseResultfreebsd ()
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 5;
/* loop from second elment to the fifths last */
for($idx = 1; $idx < $icmp_seq_count; $idx++ ) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_bytes_per_request = (int) $parts[0 ];
$this->_bytes_total = (int) ($parts[0 ] * $icmp_seq_count);
$this->_target_ip = substr($parts[3 ], 0 , -1 );
$stats = explode(',', $this->_raw_data[$raw_data_len - 2 ]);
$transmitted = explode(' ', $stats[0 ]);
$this->_transmitted = (int) $transmitted[0 ];
$received = explode(' ', $stats[1 ]);
$this->_received = (int) $received[1 ];
$this->_loss = (int) $loss[1 ];
$this->_round_trip['min'] = (float) ltrim($round_trip[4 ]);
$this->_round_trip['avg'] = (float) $round_trip[5 ];
$this->_round_trip['max'] = (float) $round_trip[6 ];
$this->_round_trip['stddev'] = (float) $round_trip[7 ];
} /* function _parseResultfreebsd() */
* Parses the output of Windows' ping command
* @author Kai Schr�der <k.schroeder@php.net>
function _parseResultwindows ()
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 8;
/* loop from fourth elment to the sixths last */
for($idx = 1; $idx <= $icmp_seq_count; $idx++ ) {
$parts = explode(' ', $this->_raw_data[$idx + 2 ]);
$this->_icmp_sequence[$idx - 1 ] = (int) substr(end(split('=', $parts[4 ])), 0 , -2 );
if ($ttl > 0 && $this->_ttl == 0 ) {
$parts = explode(' ', $this->_raw_data[1 ]);
$this->_bytes_per_request = (int) $parts[4 ];
$this->_bytes_total = $this->_bytes_per_request * $icmp_seq_count;
$this->_target_ip = substr(trim($parts[2 ]), 1 , -1 );
$stats = explode(',', $this->_raw_data[$raw_data_len - 3 ]);
$transmitted = explode('=', $stats[0 ]);
$this->_transmitted = (int) $transmitted[1 ];
$received = explode('=', $stats[1 ]);
$this->_received = (int) $received[1 ];
$this->_loss = (int) $loss[1 ];
$this->_round_trip['min'] = (int) substr(trim($round_trip[1 ]), 0 , -2 );
$this->_round_trip['max'] = (int) substr(trim($round_trip[3 ]), 0 , -2 );
$this->_round_trip['avg'] = (int) substr(trim($round_trip[5 ]), 0 , -2 );
} /* function _parseResultwindows() */
* Parses the output of sunos ping command
* @author AxL H. Ferriz <ahferriz@inamicsys.com>
function _parseResultsunos ()
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 4;
/* loop from second elment to the fifths last */
for ($idx = 1; $idx < $icmp_seq_count; $idx++ ) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_bytes_per_request = $parts[0 ];
$this->_bytes_total = (int) $parts[0 ] * $icmp_seq_count;
$this->_target_ip = substr($parts[4 ], 1 , -2 );
$this->_ttl = NULL; /* no ttl */
$stats = explode(',', $this->_raw_data[$raw_data_len - 2 ]);
$transmitted = explode(' ', $stats[0 ]);
$this->_transmitted = $transmitted[0 ];
$received = explode(' ', $stats[1 ]);
$this->_received = $received[1 ];
$this->_loss = (int) $loss[1 ];
$round_trip = explode(' ', $this->_raw_data[$raw_data_len - 1 ]);
$minavgmax = explode('/', $round_trip[4 ]);
$this->_round_trip['min'] = $minavgmax[0 ];
$this->_round_trip['avg'] = $minavgmax[1 ];
$this->_round_trip['max'] = $minavgmax[2 ];
} /* function _parseResultsunos() */
* Returns a Ping_Result property
* @param string $name property name
* @return mixed property value
return isset ($this->$name)? $this->$name: '';
} /* function getValue() */
* Accessor for $this->_target_ip;
* @return string IP address
* @see Ping_Result::_target_ip
return $this->_target_ip;
} /* function getTargetIp() */
* Accessor for $this->_icmp_sequence;
* @return array ICMP sequence
* @see Ping_Result::_icmp_sequence
function getICMPSequence ()
return $this->_icmp_sequence;
} /* function getICMPSequencs() */
* Accessor for $this->_bytes_per_request;
* @return int bytes per request
* @see Ping_Result::_bytes_per_request
function getBytesPerRequest ()
return $this->_bytes_per_request;
} /* function getBytesPerRequest() */
* Accessor for $this->_bytes_total;
* @return int total bytes
* @see Ping_Result::_bytes_total
return $this->_bytes_total;
} /* function getBytesTotal() */
* Accessor for $this->_ttl;
} /* function getTTL() */
* Accessor for $this->_raw_data;
* @see Ping_Result::_raw_data
} /* function getRawData() */
* Accessor for $this->_sysname;
* @return string OS_Guess::sysname
* @see Ping_Result::_sysname
} /* function getSystemName() */
* Accessor for $this->_round_trip;
* @return array statistical information
* @see Ping_Result::_round_trip
return $this->_round_trip;
} /* function getRoundTrip() */
* Accessor for $this->_round_trip['min'];
* @return array statistical information
* @see Ping_Result::_round_trip
return $this->_round_trip['min'];
} /* function getMin() */
* Accessor for $this->_round_trip['max'];
* @return array statistical information
* @see Ping_Result::_round_trip
return $this->_round_trip['max'];
} /* function getMax() */
* Accessor for $this->_round_trip['stddev'];
* @return array statistical information
* @see Ping_Result::_round_trip
return $this->_round_trip['stddev'];
} /* function getStddev() */
* Accessor for $this->_round_tripp['avg'];
* @return array statistical information
* @see Ping_Result::_round_trip
return $this->_round_trip['avg'];
} /* function getAvg() */
* Accessor for $this->_transmitted;
* @return array statistical information
function getTransmitted ()
return $this->_transmitted;
} /* function getTransmitted() */
* Accessor for $this->_received;
* @return array statistical information
} /* function getReceived() */
* Accessor for $this->_loss;
* @return array statistical information
} /* function getLoss() */
} /* class Net_Ping_Result */
Documentation generated on Mon, 11 Mar 2019 15:06:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|