我有使用php检查与ip的连接的代码,我在互联网上找到了此代码,但返回错误的值,它返回是否可以访问ip,但返回值0表示返回的数组中的其他值像这样
array(3) { ["result"]=> bool(true) ["last_ping_timetaken"]=> int(0) ["db"]=>
array(6) { ["xmt"]=> int(0) ["rcv"]=> int(0) ["loss"]=> int(0) ["min"]=> int(0)
["max"]=> int(0) ["avg"]=> int(0) } }
这是我使用的代码
function isPingable($hostname, $address_family = 'ipv4', $attribs = array())
{
global $config;
$response = array();
if (1 == 1) {
$fping_params = '';
$fping_params .= ' -t 50' ;
$fping_params .= ' -c 5' ;
$fping_params .= ' -p 10' ;
$status = fping($hostname, $fping_params, $address_family);
if ($status['exitcode'] > 0 || $status['loss'] == 100) {
$response['result'] = false;
} else {
$response['result'] = true;
}
if (is_numeric($status['avg'])) {
$response['last_ping_timetaken'] = $status['avg'];
}
$response['db'] = array_intersect_key($status, array_flip(array('xmt','rcv','loss','min','max','avg')));
} else {
$response['result'] = true;
$response['last_ping_timetaken'] = 0;
}
return($response);
}
function fping($host, $params, $address_family = 'ipv4')
{
global $config;
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
// Default to ipv4
$fping_path = $config['fping'];
if ($address_family == 'ipv6') {
$fping_path = $config['fping6'];
}
$process = proc_open($fping_path . ' -e -q ' .$params . ' ' .$host.' 2>&1', $descriptorspec, $pipes);
$read = '';
$proc_status = 0;
if (is_resource($process)) {
fclose($pipes[0]);
while (!feof($pipes[1])) {
$read .= fgets($pipes[1], 1024);
}
fclose($pipes[1]);
$proc_status = proc_get_status($process);
proc_close($process);
}
preg_match('/[0-9]+\/[0-9]+\/[0-9]+%/', $read, $loss_tmp);
preg_match('/[0-9\.]+\/[0-9\.]+\/[0-9\.]*$/', $read, $latency);
$loss = preg_replace("/%/", "", $loss_tmp[0]);
list($xmt,$rcv,$loss) = preg_split("/\//", $loss);
list($min,$avg,$max) = preg_split("/\//", $latency[0]);
if ($loss < 0) {
$xmt = 1;
$rcv = 1;
$loss = 100;
}
$xmt = set_numeric($xmt);
$rcv = set_numeric($rcv);
$loss = set_numeric($loss);
$min = set_numeric($min);
$max = set_numeric($max);
$avg = set_numeric($avg);
$response = array('xmt'=>$xmt,'rcv'=>$rcv,'loss'=>$loss,'min'=>$min,'max'=>$max,'avg'=>$avg,'exitcode'=>$proc_status['exitcode']);
return $response;
}
$var=isPingable('10.10.0.42');
var_dump($var);
它返回true或false,因为ip的值是否可以访问,但是每次另一个值都是0,我的错是什么以及如何解决