ping.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,099 行 · 第 1/3 页
PHP
1,099 行
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 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鰀er <k.schroeder@php.net> |
// +----------------------------------------------------------------------+
//
// $Id: Ping.php 6819 2007-06-20 13:09:21Z kevin_fourie $
require_once "PEAR.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);
/**************************TODO*******************************************/
/*
*
* - add Net_Ping_Result parser for:
* + IRIX64
* + OSF1
* + BSD/OS
* + OpenBSD
* - fix Net_Ping::checkHost()
* - reset result buffer
*/
/**
* Wrapper class for ping calls
*
* Usage:
*
* <?php
* require_once "Net/Ping.php";
* $ping = Net_Ping::factory();
* if(PEAR::isError($ping)) {
* echo $ping->getMessage();
* } else {
* $ping->setArgs(array('count' => 2));
* var_dump($ping->ping('example.com'));
* }
* ?>
*
* @author Jan Lehnardt <jan@php.net>
* @version $Revision: 6819 $
* @package Net
* @access public
*/
class Net_Ping
{
/**
* Location where the ping program is stored
*
* @var string
* @access private
*/
var $_ping_path = "";
/**
* Array with the result from the ping execution
*
* @var array
* @access private
*/
var $_result = array();
/**
* OS_Guess instance
*
* @var object
* @access private
*/
var $_OS_Guess = "";
/**
* OS_Guess->getSysname result
*
* @var string
* @access private
*/
var $_sysname = "";
/**
* Ping command arguments
*
* @var array
* @access private
*/
var $_args = array();
/**
* Indicates if an empty array was given to setArgs
*
* @var boolean
* @access private
*/
var $_noArgs = true;
/**
* Contains the argument->option relation
*
* @var array
* @access private
*/
var $_argRelation = array();
/**
* Constructor for the Class
*
* @access private
*/
function Net_Ping($ping_path, $sysname)
{
$this->_ping_path = $ping_path;
$this->_sysname = $sysname;
$this->_initArgRelation();
} /* function Net_Ping() */
/**
* Factory for Net_Ping
*
* @access public
*/
function factory()
{
$ping_path = '';
$sysname = Net_Ping::_setSystemName();
if (($ping_path = Net_Ping::_setPingPath($sysname)) == NET_PING_CANT_LOCATE_PING_BINARY) {
return PEAR::throwError(NET_PING_CANT_LOCATE_PING_BINARY_MSG, NET_PING_CANT_LOCATE_PING_BINARY);
} else {
return new Net_Ping($ping_path, $sysname);
}
} /* function factory() */
/**
* Resolve the system name
*
* @access private
*/
function _setSystemName()
{
$OS_Guess = new OS_Guess;
$sysname = $OS_Guess->getSysname();
/* Nasty hack for Debian, as it uses a custom ping version */
if ('linux' == $sysname) {
if (file_exists('/etc/debian_version')) {
$sysname = 'debian';
}
}
return $sysname;
} /* function _setSystemName */
/**
* Set the arguments array
*
* @param array $args Hash with options
* @return mixed true or PEAR_error
* @access public
*/
function setArgs($args)
{
if (!is_array($args)) {
return PEAR::throwError(NET_PING_INVALID_ARGUMENTS_MSG, NET_PING_INVALID_ARGUMENTS);
}
$this->_setNoArgs($args);
$this->_args = $args;
return true;
} /* function setArgs() */
/**
* Set the noArgs flag
*
* @param array $args Hash with options
* @return void
* @access private
*/
function _setNoArgs($args)
{
if (0 == count($args)) {
$this->_noArgs = true;
} else {
$this->_noArgs = false;
}
} /* function _setNoArgs() */
/**
* Sets the system's path to the ping binary
*
* @access private
*/
function _setPingPath($sysname)
{
$status = '';
$output = array();
$ping_path = '';
if ("windows" == $sysname) {
return "ping";
} else {
$ping_path = exec("which ping", $output, $status);
if (0 != $status) {
return NET_PING_CANT_LOCATE_PING_BINARY;
} else {
return $ping_path;
}
}
} /* function _setPingPath() */
/**
* Creates the argument list according to platform differences
*
* @return string Argument line
* @access private
*/
function _createArgList()
{
$retval = array();
$timeout = "";
$iface = "";
$ttl = "";
$count = "";
$quiet = "";
$size = "";
$seq = "";
$deadline = "";
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) {
case "sunos":
if ($size || $count || $iface) {
/* $size and $count must be _both_ defined */
$seq = " -s ";
if ($size == "") {
$size = " 56 ";
}
if ($count == "") {
$count = " 5 ";
}
}
$retval['pre'] = $iface.$seq.$ttl;
$retval['post'] = $size.$count;
break;
case "freebsd":
$retval['pre'] = $quiet.$count.$ttl.$timeout;
$retval['post'] = "";
break;
case "darwin":
$retval['pre'] = $count.$timeout.$size;
$retval['post'] = "";
break;
case "netbsd":
$retval['pre'] = $quiet.$count.$iface.$size.$ttl.$timeout;
$retval['post'] = "";
break;
case "linux":
$retval['pre'] = $quiet.$deadline.$count.$ttl.$size.$timeout;
$retval['post'] = "";
break;
case "debian":
$retval['pre'] = $quiet.$count.$ttl.$size.$timeout;
$retval['post'] = "";
/* undo nasty debian hack*/
$this->_sysname = 'linux';
break;
case "windows":
$retval['pre'] = $count.$ttl.$timeout;
$retval['post'] = "";
break;
case "hpux":
$retval['pre'] = $ttl;
$retval['post'] = $size.$count;
break;
case "aix":
$retval['pre'] = $count.$timeout.$ttl.$size;
$retval['post'] = "";
break;
default:
$retval['pre'] = "";
$retval['post'] = "";
break;
}
return($retval);
} /* function _createArgList() */
/**
* Execute ping
*
* @param string $host hostname
* @return mixed String on error or array with the result
* @access public
*/
function ping($host)
{
if($this->_noArgs) {
$this->setArgs(array('count' => 3));
}
$argList = $this->_createArgList();
$cmd = $this->_ping_path." ".$argList['pre']." ".$host." ".$argList['post'];
exec($cmd, $this->_result);
if (!is_array($this->_result)) {
return PEAR::throwError(NET_PING_FAILED_MSG, NET_PING_FAILED);
}
if (count($this->_result) == 0) {
return PEAR::throwError(NET_PING_HOST_NOT_FOUND_MSG, NET_PING_HOST_NOT_FOUND);
} else {
return Net_Ping_Result::factory($this->_result, $this->_sysname);
}
} /* function ping() */
/**
* 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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?