- 论坛徽章:
- 0
|
在pear的Net_Ping中,如何将获取的ping信息提取出来?
<?php
require_once 'Net/Ping.php' ;
$ping = new Net_Ping;
$ping->setArgs(array("count" => 5,
"size" => 32,
"ttl" => 512
)
);
var_dump($ping->ping("10.44.98.20" );
?>
执行以上代码,返回以下信息:
object(Net_Ping_Result)#2 (11) { ["_icmp_sequence"]=> array(2) { [0]=> int(1533) [1]=> int(73 } ["_target_ip"]=> string(2) "it" ["_bytes_per_request"]=> int(0) ["_bytes_total"]=> int(0) ["_ttl"]=> int(61) ["_raw_data"]=> array(10) { [0]=> string(0) "" [1]=> string(42) " inging 10.44.98.20 with 32 bytes of data:" [2]=> string(0) "" [3]=> string(51) "Reply from 10.44.98.20: bytes=32 time=1533ms TTL=61" [4]=> string(50) "Reply from 10.44.98.20: bytes=32 time=738ms TTL=61" [5]=> string(0) "" [6]=> string(32) " ing statistics for 10.44.98.20:" [7]=> string(56) " Packets: Sent = 2, Received = 2, Lost = 0 (0% loss)," [8]=> string(46) "Approximate round trip times in milli-seconds:" [9]=> string(55) " Minimum = 738ms, Maximum = 1533ms, Average = 1135ms" } ["_sysname"]=> string(7) "windows" ["_round_trip"]=> array(3) { ["min"]=> int(73 ["max"]=> int(1533) ["avg"]=> int(1135) } ["_transmitted"]=> int(2) ["_received"]=> int(2) ["_loss"]=> int(0) }
如何才能将上面的信息按我需要的值如Sent包,Received包, Lost值, time,TTL提取出来?不是从返回的信息中取,而是从ping的函数中取。请指教。。。
ping.php如下:
require_once " EAR.php";
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_FAILED', 0);
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);
class Net_Ping
{
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
} /* class Net_Ping */
class Net_Ping_Result
{
var $_icmp_sequence = array(); /* array($sequence_number => $time ) */
var $_target_ip;
var $_bytes_per_request;
var $_bytes_total;
var $_ttl;
var $_raw_data = array();
var $_sysname;
var $_round_trip = array(); /* array('min' => xxx, 'avg' => yyy, 'max' => zzz) */
function Net_Ping_Result($result, $sysname)
{
$this->_raw_data = $result;
$this->_sysname = $sysname;
$this->_parseResult();
} /* function Net_Ping_Result() */
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() */
function _prepareParseResult($sysname)
{
$parse_methods = array_values(array_map('strtolower', get_class_methods('Net_Ping_Result')));
return in_array('_parseresult'.$sysname, $parse_methods);
} /* function _prepareParseResult() */
function _parseResult()
{
call_user_func(array(&$this, '_parseResult'.$this->_sysname));
} /* function _parseResult() */
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];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
$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
*
* @access private
*/
function _parseResulthpux()
{
$parts = array();
$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[(int)substr($parts[4], 9, strlen($parts[4]))] = (int)substr($parts[5], 5, strlen($parts[5]));
}
$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];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/',$this->_raw_data[$raw_data_len - 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
*
* @access private
*/
function _parseResultaix()
{
$parts = array();
$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[(int)substr($parts[4], 9, strlen($parts[4]))] = (int)substr($parts[6], 5, strlen($parts[6]));
}
$this->_bytes_per_request = (int)$parts[0];
$this->_bytes_total = (int)($parts[0] * $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];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/',$this->_raw_data[$raw_data_len - 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
*
* @access private
*/
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->_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)($parts[0] * $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];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
$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鰀er <k.schroeder@php.net>
* @access private
*/
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);
$ttl = (int)substr($parts[5], 4, strlen($parts[3]));
if ($ttl > 0 && $this->_ttl == 0) {
$this->_ttl = $ttl;
}
}
$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];
$loss = explode('=', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode(',', str_replace('=', ',', $this->_raw_data[$raw_data_len - 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() */
/**
* Returns a Ping_Result property
*
* @param string $name property name
* @return mixed property value
* @access public
*/
function getValue($name)
{
return isset($this->$name)?$this->$name:'';
} /* function getValue() */
/**
* Accessor for $this->_target_ip;
*
* @return string IP address
* @access public
* @see Ping_Result::_target_ip
*/
function getTargetIp()
{
return $this->_target_ip;
} /* function getTargetIp() */
/**
* Accessor for $this->_icmp_sequence;
*
* @return array ICMP sequence
* @access private
* @see Ping_Result::_icmp_sequence
*/
function getICMPSequence()
{
return $this->_icmp_sequence;
} /* function getICMPSequencs() */
/**
* Accessor for $this->_bytes_per_request;
*
* @return int bytes per request
* @access private
* @see Ping_Result::_bytes_per_request
*/
function getBytesPerRequest()
{
return $this->_bytes_per_request;
} /* function getBytesPerRequest() */
/**
* Accessor for $this->_bytes_total;
*
* @return int total bytes
* @access private
* @see Ping_Result::_bytes_total
*/
function getBytesTotal()
{
return $this->_bytes_total;
} /* function getBytesTotal() */
/**
* Accessor for $this->_ttl;
*
* @return int TTL
* @access private
* @see Ping_Result::_ttl
*/
function getTTL()
{
return $this->_ttl;
} /* function getTTL() */
/**
* Accessor for $this->_raw_data;
*
* @return array raw data
* @access private
* @see Ping_Result::_raw_data
*/
function getRawData()
{
return $this->_raw_data;
} /* function getRawData() */
/**
* Accessor for $this->_sysname;
*
* @return string OS_Guess::sysname
* @access private
* @see Ping_Result::_sysname
*/
function getSystemName()
{
return $this->_sysname;
} /* function getSystemName() */
/**
* Accessor for $this->_round_trip;
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getRoundTrip()
{
return $this->_round_trip;
} /* function getRoundTrip() */
/**
* Accessor for $this->_round_trip['min'];
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getMin()
{
return $this->_round_trip['min'];
} /* function getMin() */
/**
* Accessor for $this->_round_trip['max'];
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getMax()
{
return $this->_round_trip['max'];
} /* function getMax() */
/**
* Accessor for $this->_round_trip['stddev'];
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getStddev()
{
return $this->_round_trip['stddev'];
} /* function getStddev() */
/**
* Accessor for $this->_round_tripp['avg'];
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getAvg()
{
return $this->_round_trip['avg'];
} /* function getAvg() */
/**
* Accessor for $this->_transmitted;
*
* @return array statistical information
* @access private
*/
function getTransmitted()
{
return $this->_transmitted;
} /* function getTransmitted() */
/**
* Accessor for $this->_received;
*
* @return array statistical information
* @access private
*/
function getReceived()
{
return $this->_received;
} /* function getReceived() */
/**
* Accessor for $this->_loss;
*
* @return array statistical information
* @access private
*/
function getLoss()
{
return $this->_loss;
} /* function getLoss() */
} /* class Net_Ping_Result */
?> |
|