ping.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,099 行 · 第 1/3 页
PHP
1,099 行
* @return bool True on success or false otherwise
*
*/
function checkHost($host, $severely = true)
{
$matches = array();
$this->setArgs(array("count" => 10,
"size" => 32,
"quiet" => null,
"deadline" => 10
)
);
$res = $this->ping($host);
if (PEAR::isError($res)) {
return false;
}
if (!preg_match_all('|\d+|', $res[3], $matches) || count($matches[0]) < 3) {
ob_start();
$rep = ob_get_contents();
ob_end_clean();
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");
return false;
}
if ($matches[0][1] == 0) {
return false;
}
// [0] => transmitted, [1] => received
if ($matches[0][0] != $matches[0][1] && $severely) {
return false;
}
return true;
} /* 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
* @return bool false
* @access private
* @author Kai Schr鰀er <k.schroeder@php.net>
*/
function _raiseError($error)
{
if (PEAR::isError($error)) {
$error = $error->getMessage();
}
trigger_error($error, E_USER_WARNING);
return false;
} /* function _raiseError() */
/**
* Creates the argument list according to platform differences
*
* @return string Argument line
* @access private
*/
function _initArgRelation()
{
$this->_argRelation["sunos"] = array(
"timeout" => NULL,
"ttl" => "-t",
"count" => " ",
"quiet" => "-q",
"size" => " ",
"iface" => "-i"
);
$this->_argRelation["freebsd"] = array (
"timeout" => "-t",
"ttl" => "-m",
"count" => "-c",
"quiet" => "-q",
"size" => NULL,
"iface" => NULL
);
$this->_argRelation["netbsd"] = array (
"timeout" => "-w",
"iface" => "-I",
"ttl" => "-T",
"count" => "-c",
"quiet" => "-q",
"size" => "-s"
);
$this->_argRelation["openbsd"] = array (
"timeout" => "-w",
"iface" => "-I",
"ttl" => "-t",
"count" => "-c",
"quiet" => "-q",
"size" => "-s"
);
$this->_argRelation["darwin"] = array (
"timeout" => "-t",
"iface" => NULL,
"ttl" => NULL,
"count" => "-c",
"quiet" => "-q",
"size" => NULL
);
$this->_argRelation["linux"] = array (
"timeout" => "-t",
"iface" => NULL,
"ttl" => "-m",
"count" => "-c",
"quiet" => "-q",
"size" => "-s",
"deadline" => "-w"
);
$this->_argRelation["debian"] = array (
"timeout" => "-t",
"iface" => NULL,
"ttl" => "-m",
"count" => "-c",
"quiet" => "-q",
"size" => "-s",
);
$this->_argRelation["windows"] = array (
"timeout" => "-w",
"iface" => NULL,
"ttl" => "-i",
"count" => "-n",
"quiet" => NULL,
"size" => "-l"
);
$this->_argRelation["hpux"] = array (
"timeout" => NULL,
"iface" => NULL,
"ttl" => "-t",
"count" => "-n",
"quiet" => NULL,
"size" => " "
);
$this->_argRelation["aix"] = array (
"timeout" => "-i",
"iface" => NULL,
"ttl" => "-T",
"count" => "-c",
"quiet" => NULL,
"size" => "-s"
);
} /* function _initArgRelation() */
} /* class Net_Ping */
/**
* Container class for Net_Ping results
*
* @author Jan Lehnardt <jan@php.net>
* @version $Revision: 6819 $
* @package Net
* @access private
*/
class Net_Ping_Result
{
/**
* ICMP sequence number and associated time in ms
*
* @var array
* @access private
*/
var $_icmp_sequence = array(); /* array($sequence_number => $time ) */
/**
* The target's IP Address
*
* @var string
* @access private
*/
var $_target_ip;
/**
* Number of bytes that are sent with each ICMP request
*
* @var int
* @access private
*/
var $_bytes_per_request;
/**
* The total number of bytes that are sent with all ICMP requests
*
* @var int
* @access private
*/
var $_bytes_total;
/**
* The ICMP request's TTL
*
* @var int
* @access private
*/
var $_ttl;
/**
* The raw Net_Ping::result
*
* @var array
* @access private
*/
var $_raw_data = array();
/**
* The Net_Ping::_sysname
*
* @var int
* @access private
*/
var $_sysname;
/**
* Statistical information about the ping
*
* @var int
* @access private
*/
var $_round_trip = array(); /* array('min' => xxx, 'avg' => yyy, 'max' => zzz) */
/**
* Constructor for the Class
*
* @access private
*/
function Net_Ping_Result($result, $sysname)
{
$this->_raw_data = $result;
$this->_sysname = $sysname;
$this->_parseResult();
} /* function Net_Ping_Result() */
/**
* Factory for Net_Ping_Result
*
* @access public
* @param array $result Net_Ping result
* @param string $sysname OS_Guess::sysname
*/
function factory($result, $sysname)
{
if (!Net_Ping_Result::_prepareParseResult($sysname)) {
return PEAR::throwError(NET_PING_RESULT_UNSUPPORTED_BACKEND_MSG, NET_PING_RESULT_UNSUPPORTED_BACKEND);
} else {
return new Net_Ping_Result($result, $sysname);
}
} /* function factory() */
/**
* Preparation method for _parseResult
*
* @access private
* @param string $sysname OS_Guess::sysname
* $return bool
*/
function _prepareParseResult($sysname)
{
$parse_methods = array_values(get_class_methods('Net_Ping_Result'));
return in_array('_parseresult'.$sysname, $parse_methods);
} /* function _prepareParseResult() */
/**
* Delegates the parsing routine according to $this->_sysname
*
* @access private
*/
function _parseResult()
{
call_user_func(array(&$this, '_parseResult'.$this->_sysname));
} /* function _parseResult() */
/**
* Parses the output of Linux' ping command
*
* @access private
* @see _parseResultlinux
*/
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->_icmp_sequence[substr(@$parts[4], 9, strlen(@$parts[4]))] = substr(@$parts[6], 5, strlen(@$parts[6]));
}
$this->_bytes_per_request = $parts[0];
$this->_bytes_total = (int)$parts[0] * $icmp_seq_count;
$this->_target_ip = substr($parts[3], 0, -1);
$this->_ttl = substr($parts[5], 4, strlen($parts[3]));
$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];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
/* if mdev field exists, shift input one unit left */
if (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];
} else {
$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
*
* @access private
* @see _parseResultfreebsd
*/
function _parseResultnetbsd()
{
$this->_parseResultfreebsd();
} /* function _parseResultnetbsd() */
/**
* Parses the output of Darwin's ping command
*
* @access private
*/
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->_icmp_sequence[substr($parts[4], 9, strlen($parts[4]))] = substr($parts[6], 5, strlen($parts[6]));
}
$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);
$this->_ttl = (int)substr($parts[5], 4, strlen($parts[3]));
$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];
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?