/** * 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'] . " " . escapeshellcmd($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 (!is_array($this->_result)) { return PEAR::raiseError(NET_PING_FAILED_MSG, NET_PING_FAILED); } if (count($this->_result) == 0) { return PEAR::raiseError(NET_PING_HOST_NOT_FOUND_MSG, NET_PING_HOST_NOT_FOUND); } else { // Here we pass $this->_sysname to the factory(), but it is // not actually used by the class. It's only maintained in // the Net_Ping_Result class because the // Net_Ping_Result::getSysName() method needs to be retained // for backwards compatibility. return Net_Ping_Result::factory($this->_result, $this->_sysname); } }
/** * 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 test_net_ping($os, $result, $expect) { $ping = Net_Ping_Result::factory($result, $os); if (PEAR::isError($ping)) { echo failure($os, "factory(): " . $ping->getMessage()); } if ($expect['min'] !== $ping->getMin()) { echo failure($os, "getMin()"); } if ($expect['avg'] !== $ping->getAvg()) { echo failure($os, "getAvg()"); } if ($expect['max'] !== $ping->getMax()) { echo failure($os, "getMax()"); } if ($expect['stddev'] !== $ping->getStddev()) { echo failure($os, "getStddev()"); } if ($os !== $ping->getSystemName()) { echo failure($os, "getSystemName()"); } if ($expect['ttl'] !== $ping->getTTL()) { echo failure($os, "getTTL()"); } if (!is_array($ping->getICMPSequence())) { echo failure($os, "getICMPSequence()"); } if ($expect['transmitted'] !== $ping->getTransmitted()) { echo failure($os, "getTransmitted()"); } if ($expect['received'] !== $ping->getReceived()) { echo failure($os, "getReceived()"); } if ($expect['bytesperreq'] !== $ping->getBytesPerRequest()) { echo failure($os, "getBytesPerRequest()"); } if ($expect['bytestotal'] !== $ping->getBytesTotal()) { echo failure($os, "getBytesTotal()"); } if ($expect['targetip'] !== $ping->getTargetIp()) { echo failure($os, "getTargetIp()"); } if ($expect['loss'] !== $ping->getLoss()) { echo failure($os, "getLoss()"); } unset($ping); }