Net
[ class tree: Net ] [ index: Net ] [ all elements ]

Source for file Ping.php

Documentation is available at Ping.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  18. // |          Jan Lehnardt  <jan@php.net>                                 |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Ping.php,v 1.39 2005/12/19 21:49:29 jan Exp $
  22.  
  23. require_once "PEAR.php";
  24. require_once "OS/Guess.php";
  25.  
  26. define('NET_PING_FAILED_MSG',                     'execution of ping failed'        );
  27. define('NET_PING_HOST_NOT_FOUND_MSG',             'unknown host'                    );
  28. define('NET_PING_INVALID_ARGUMENTS_MSG',          'invalid argument array'          );
  29. define('NET_PING_CANT_LOCATE_PING_BINARY_MSG',    'unable to locate the ping binary');
  30. define('NET_PING_RESULT_UNSUPPORTED_BACKEND_MSG''Backend not Supported'           );
  31.  
  32. define('NET_PING_FAILED',                     0);
  33. define('NET_PING_HOST_NOT_FOUND',             1);
  34. define('NET_PING_INVALID_ARGUMENTS',          2);
  35. define('NET_PING_CANT_LOCATE_PING_BINARY',    3);
  36. define('NET_PING_RESULT_UNSUPPORTED_BACKEND'4);
  37.  
  38.  
  39. /* TODO
  40.  *
  41.  * - add Net_Ping_Result parser for:
  42.  *   + IRIX64
  43.  *   + OSF1
  44.  *   + BSD/OS
  45.  *   + OpenBSD
  46.  * - fix Net_Ping::checkHost()
  47.  * - reset result buffer
  48.  */
  49.  
  50. /**
  51. * Wrapper class for ping calls
  52. *
  53. * Usage:
  54. *
  55. * <?php
  56. *   require_once "Net/Ping.php";
  57. *   $ping = Net_Ping::factory();
  58. *   if(PEAR::isError($ping)) {
  59. *     echo $ping->getMessage();
  60. *   } else {
  61. *     $ping->setArgs(array('count' => 2));
  62. *     var_dump($ping->ping('example.com'));
  63. *   }
  64. * ?>
  65. *
  66. @author   Jan Lehnardt <jan@php.net>
  67. @version  $Revision: 1.39 $
  68. @package  Net
  69. @access   public
  70. */
  71. class Net_Ping
  72. {
  73.     /**
  74.     * Location where the ping program is stored
  75.     *
  76.     * @var string 
  77.     * @access private
  78.     */
  79.     var $_ping_path "";
  80.  
  81.     /**
  82.     * Array with the result from the ping execution
  83.     *
  84.     * @var array 
  85.     * @access private
  86.     */
  87.     var $_result = array();
  88.  
  89.     /**
  90.     * OS_Guess instance
  91.     *
  92.     * @var object 
  93.     * @access private
  94.     */
  95.     var $_OS_Guess "";
  96.  
  97.     /**
  98.     * OS_Guess->getSysname result
  99.     *
  100.     * @var string 
  101.     * @access private
  102.     */
  103.     var $_sysname "";
  104.  
  105.     /**
  106.     * Ping command arguments
  107.     *
  108.     * @var array 
  109.     * @access private
  110.     */
  111.     var $_args = array();
  112.  
  113.     /**
  114.     * Indicates if an empty array was given to setArgs
  115.     *
  116.     * @var boolean 
  117.     * @access private
  118.     */
  119.     var $_noArgs = true;
  120.  
  121.     /**
  122.     * Contains the argument->option relation
  123.     *
  124.     * @var array 
  125.     * @access private
  126.     */
  127.     var $_argRelation = array();
  128.  
  129.     /**
  130.     * Constructor for the Class
  131.     *
  132.     * @access private
  133.     */
  134.     function Net_Ping($ping_path$sysname)
  135.     {
  136.         $this->_ping_path $ping_path;
  137.         $this->_sysname   $sysname;
  138.         $this->_initArgRelation();
  139.     /* function Net_Ping() */
  140.  
  141.     /**
  142.     * Factory for Net_Ping
  143.     *
  144.     * @access public
  145.     */
  146.     function factory()
  147.     {
  148.         $ping_path '';
  149.  
  150.         $sysname Net_Ping::_setSystemName();
  151.  
  152.         if (($ping_path Net_Ping::_setPingPath($sysname)) == NET_PING_CANT_LOCATE_PING_BINARY{
  153.             return PEAR::throwError(NET_PING_CANT_LOCATE_PING_BINARY_MSGNET_PING_CANT_LOCATE_PING_BINARY);
  154.         else {
  155.             return new Net_Ping($ping_path$sysname);
  156.         }
  157.     /* function factory() */
  158.  
  159.     /** 
  160.      * Resolve the system name
  161.      *
  162.      * @access private
  163.      */
  164.     function _setSystemName()
  165.     {
  166.         $OS_Guess  = new OS_Guess;
  167.         $sysname   $OS_Guess->getSysname();
  168.  
  169.         /* Nasty hack for Debian, as it uses a custom ping version */
  170.         if ('linux' == $sysname{
  171.             if (file_exists('/etc/debian_version')) {
  172.                 $sysname 'debian';
  173.             }
  174.         }
  175.  
  176.         return $sysname;
  177.         
  178.     /* function _setSystemName */
  179.  
  180.     /**
  181.     * Set the arguments array
  182.     *
  183.     * @param array $args Hash with options
  184.     * @return mixed true or PEAR_error
  185.     * @access public
  186.     */
  187.     function setArgs($args)
  188.     {
  189.         if (!is_array($args)) {
  190.             return PEAR::throwError(NET_PING_INVALID_ARGUMENTS_MSGNET_PING_INVALID_ARGUMENTS);
  191.         }
  192.  
  193.         $this->_setNoArgs($args);
  194.  
  195.         $this->_args $args;
  196.  
  197.         return true;
  198.     /* function setArgs() */
  199.  
  200.     /**
  201.     * Set the noArgs flag
  202.     *
  203.     * @param array $args Hash with options
  204.     * @return void 
  205.     * @access private
  206.     */
  207.     function _setNoArgs($args)
  208.     {
  209.         if (0 == count($args)) {
  210.             $this->_noArgs = true;
  211.         else {
  212.             $this->_noArgs = false;
  213.         }
  214.     /* function _setNoArgs() */
  215.  
  216.     /**
  217.     * Sets the system's path to the ping binary
  218.     *
  219.     * @access private
  220.     */
  221.     function _setPingPath($sysname)
  222.     {
  223.         $status    '';
  224.         $output    = array();
  225.         $ping_path '';
  226.  
  227.         if ("windows" == $sysname{
  228.             return "ping";
  229.         else {
  230.             $ping_path exec("which ping"$output$status);
  231.             if (0 != $status{
  232.                 return NET_PING_CANT_LOCATE_PING_BINARY;
  233.             else {
  234.                 return $ping_path;
  235.             }
  236.         }
  237.     /* function _setPingPath() */
  238.  
  239.     /**
  240.     * Creates the argument list according to platform differences
  241.     *
  242.     * @return string Argument line
  243.     * @access private
  244.     */
  245.     function _createArgList()
  246.     {
  247.         $retval     = array();
  248.  
  249.         $timeout    "";
  250.         $iface      "";
  251.         $ttl        "";
  252.         $count      "";
  253.         $quiet      "";
  254.         $size       "";
  255.         $seq        "";
  256.         $deadline   "";
  257.  
  258.         foreach($this->_args AS $option => $value{
  259.             if(!empty($option&& isset($this->_argRelation[$this->_sysname][$option]&& NULL != $this->_argRelation[$this->_sysname][$option]{
  260.                 ${$option$this->_argRelation[$this->_sysname][$option]." ".$value." ";
  261.              }
  262.         }
  263.  
  264.         switch($this->_sysname{
  265.  
  266.         case "sunos":
  267.              if ($size || $count || $iface{
  268.                  /* $size and $count must be _both_ defined */
  269.                  $seq " -s ";
  270.                  if ($size == ""{
  271.                      $size " 56 ";
  272.                  }
  273.                  if ($count == ""{
  274.                      $count " 5 ";
  275.                  }
  276.              }
  277.              $retval['pre'$iface.$seq.$ttl;
  278.              $retval['post'$size.$count;
  279.              break;
  280.  
  281.         case "freebsd":
  282.              $retval['pre'$quiet.$count.$ttl.$timeout;
  283.              $retval['post'"";
  284.              break;
  285.  
  286.         case "darwin":
  287.              $retval['pre'$count.$timeout.$size;
  288.              $retval['post'"";
  289.              break;
  290.  
  291.         case "netbsd":
  292.              $retval['pre'$quiet.$count.$iface.$size.$ttl.$timeout;
  293.              $retval['post'"";
  294.              break;
  295.  
  296.         case "linux":
  297.              $retval['pre'$quiet.$deadline.$count.$ttl.$size.$timeout;
  298.              $retval['post'"";
  299.              break;
  300.  
  301.         case "debian":
  302.              $retval['pre'$quiet.$count.$ttl.$size.$timeout;
  303.              $retval['post'"";
  304.  
  305.              /* undo nasty debian hack*/
  306.              $this->_sysname 'linux';
  307.              break;
  308.  
  309.         case "windows":
  310.              $retval['pre'$count.$ttl.$timeout;
  311.              $retval['post'"";
  312.              break;
  313.  
  314.         case "hpux":
  315.              $retval['pre'$ttl;
  316.              $retval['post'$size.$count;
  317.              break;
  318.  
  319.         case "aix":
  320.             $retval['pre'$count.$timeout.$ttl.$size;
  321.             $retval['post'"";
  322.             break;
  323.  
  324.         default:
  325.              $retval['pre'"";
  326.              $retval['post'"";
  327.              break;
  328.         }
  329.         return($retval);
  330.     }  /* function _createArgList() */
  331.  
  332.     /**
  333.     * Execute ping
  334.     *
  335.     * @param  string    $host   hostname
  336.     * @return mixed  String on error or array with the result
  337.     * @access public
  338.     */
  339.     function ping($host)
  340.     {
  341.         
  342.         if($this->_noArgs{
  343.             $this->setArgs(array('count' => 3));
  344.         }
  345.  
  346.         $argList $this->_createArgList();
  347.         $cmd $this->_ping_path." ".$argList['pre']." ".$host." ".$argList['post'];
  348.         exec($cmd$this->_result);
  349.  
  350.         if (!is_array($this->_result)) {
  351.             return PEAR::throwError(NET_PING_FAILED_MSGNET_PING_FAILED);
  352.         }
  353.  
  354.         if (count($this->_result== 0{
  355.             return PEAR::throwError(NET_PING_HOST_NOT_FOUND_MSGNET_PING_HOST_NOT_FOUND);
  356.         else {
  357.             return Net_Ping_Result::factory($this->_result$this->_sysname);
  358.         }
  359.     /* function ping() */
  360.  
  361.     /**
  362.     * Check if a host is up by pinging it
  363.     *
  364.     * @param string $host   The host to test
  365.     * @param bool $severely If some of the packages did reach the host
  366.     *                        and severely is false the function will return true
  367.     * @return bool True on success or false otherwise
  368.     *
  369.     */
  370.     function checkHost($host$severely = true)
  371.     {
  372.         $matches = array();
  373.         
  374.         $this->setArgs(array("count" => 10,
  375.                              "size"  => 32,
  376.                              "quiet" => null,
  377.                              "deadline" => 10
  378.                              )
  379.                        );
  380.         $res $this->ping($host);
  381.         if (PEAR::isError($res)) {
  382.             return false;
  383.         }
  384.         if (!preg_match_all('|\d+|'$res[3]$matches|| count($matches[0]< 3{
  385.             ob_start();
  386.             $rep ob_get_contents();
  387.             ob_end_clean();
  388.             trigger_error("Output format seems not to be supported, please report ".
  389.                           "the following to pear-dev@lists.php.net, including your ".
  390.                           "version of ping:\n $rep");
  391.             return false;
  392.         }
  393.         if ($matches[0][1== 0{
  394.             return false;
  395.         }
  396.         // [0] => transmitted, [1] => received
  397.         if ($matches[0][0!= $matches[0][1&& $severely{
  398.             return false;
  399.         }
  400.         return true;
  401.     /* function checkHost() */
  402.  
  403.     /**
  404.     * Output errors with PHP trigger_error(). You can silence the errors
  405.     * with prefixing a "@" sign to the function call: @Net_Ping::ping(..);
  406.     *
  407.     * @param mixed $error a PEAR error or a string with the error message
  408.     * @return bool false
  409.     * @access private
  410.     * @author
  411.     */
  412.     function _raiseError($error)
  413.     {
  414.         if (PEAR::isError($error)) {
  415.             $error $error->getMessage();
  416.         }
  417.         trigger_error($errorE_USER_WARNING);
  418.         return false;
  419.     }  /* function _raiseError() */
  420.  
  421.     /**
  422.     * Creates the argument list according to platform differences
  423.     *
  424.     * @return string Argument line
  425.     * @access private
  426.     */
  427.     function _initArgRelation()
  428.     {
  429.         $this->_argRelation["sunos"= array(
  430.                                              "timeout"   => NULL,
  431.                                              "ttl"       => "-t",
  432.                                              "count"     => " ",
  433.                                              "quiet"     => "-q",
  434.                                              "size"      => " ",
  435.                                              "iface"     => "-i"
  436.                                              );
  437.  
  438.         $this->_argRelation["freebsd"= array (
  439.                                                 "timeout"   => "-t",
  440.                                                 "ttl"       => "-m",
  441.                                                 "count"     => "-c",
  442.                                                 "quiet"     => "-q",
  443.                                                 "size"      => NULL,
  444.                                                 "iface"     => NULL
  445.                                                 );
  446.  
  447.         $this->_argRelation["netbsd"= array (
  448.                                                "timeout"   => "-w",
  449.                                                "iface"     => "-I",
  450.                                                "ttl"       => "-T",
  451.                                                "count"     => "-c",
  452.                                                "quiet"     => "-q",
  453.                                                "size"      => "-s"
  454.                                                );
  455.  
  456.         $this->_argRelation["openbsd"= array (
  457.                                                 "timeout"   => "-w",
  458.                                                 "iface"     => "-I",
  459.                                                 "ttl"       => "-t",
  460.                                                 "count"     => "-c",
  461.                                                 "quiet"     => "-q",
  462.                                                 "size"      => "-s"
  463.                                                 );
  464.  
  465.         $this->_argRelation["darwin"= array (
  466.                                                "timeout"   => "-t",
  467.                                                "iface"     => NULL,
  468.                                                "ttl"       => NULL,
  469.                                                "count"     => "-c",
  470.                                                "quiet"     => "-q",
  471.                                                "size"      => NULL
  472.                                                );
  473.  
  474.         $this->_argRelation["linux"= array (
  475.                                               "timeout"   => "-t",
  476.                                               "iface"     => NULL,
  477.                                               "ttl"       => "-m",
  478.                                               "count"     => "-c",
  479.                                               "quiet"     => "-q",
  480.                                               "size"      => "-s",
  481.                                               "deadline"  => "-w"
  482.                                               );
  483.  
  484.         $this->_argRelation["debian"= array (
  485.                                               "timeout"   => "-t",
  486.                                               "iface"     => NULL,
  487.                                               "ttl"       => "-m",
  488.                                               "count"     => "-c",
  489.                                               "quiet"     => "-q",
  490.                                               "size"      => "-s",
  491.                                               );
  492.  
  493.         $this->_argRelation["windows"= array (
  494.                                                 "timeout"   => "-w",
  495.                                                 "iface"     => NULL,
  496.                                                 "ttl"       => "-i",
  497.                                                 "count"     => "-n",
  498.                                                 "quiet"     => NULL,
  499.                                                 "size"      => "-l"
  500.                                                  );
  501.  
  502.         $this->_argRelation["hpux"= array (
  503.                                              "timeout"   => NULL,
  504.                                              "iface"     => NULL,
  505.                                              "ttl"       => "-t",
  506.                                              "count"     => "-n",
  507.                                              "quiet"     => NULL,
  508.                                              "size"      => " "
  509.                                              );
  510.  
  511.         $this->_argRelation["aix"= array (
  512.                                             "timeout"   => "-i",
  513.                                             "iface"     => NULL,
  514.                                             "ttl"       => "-T",
  515.                                             "count"     => "-c",
  516.                                             "quiet"     => NULL,
  517.                                             "size"      => "-s"
  518.                                             );
  519.     }  /* function _initArgRelation() */
  520. /* class Net_Ping */
  521.  
  522. /**
  523. * Container class for Net_Ping results
  524. *
  525. @author   Jan Lehnardt <jan@php.net>
  526. @version  $Revision: 1.39 $
  527. @package  Net
  528. @access   private
  529. */
  530. class Net_Ping_Result
  531. {
  532.     /**
  533.     * ICMP sequence number and associated time in ms
  534.     *
  535.     * @var array 
  536.     * @access private
  537.     */
  538.     var $_icmp_sequence = array()/* array($sequence_number => $time ) */
  539.  
  540.     /**
  541.     * The target's IP Address
  542.     *
  543.     * @var string 
  544.     * @access private
  545.     */
  546.     var $_target_ip;
  547.  
  548.     /**
  549.     * Number of bytes that are sent with each ICMP request
  550.     *
  551.     * @var int 
  552.     * @access private
  553.     */
  554.     var $_bytes_per_request;
  555.  
  556.     /**
  557.     * The total number of bytes that are sent with all ICMP requests
  558.     *
  559.     * @var int 
  560.     * @access private
  561.     */
  562.     var $_bytes_total;
  563.  
  564.     /**
  565.     * The ICMP request's TTL
  566.     *
  567.     * @var int 
  568.     * @access private
  569.     */
  570.     var $_ttl;
  571.  
  572.     /**
  573.     * The raw Net_Ping::result
  574.     *
  575.     * @var array 
  576.     * @access private
  577.     */
  578.     var $_raw_data = array();
  579.  
  580.     /**
  581.     * The Net_Ping::_sysname
  582.     *
  583.     * @var int 
  584.     * @access private
  585.     */
  586.     var $_sysname;
  587.  
  588.     /**
  589.     * Statistical information about the ping
  590.     *
  591.     * @var int 
  592.     * @access private
  593.     */
  594.     var $_round_trip = array()/* array('min' => xxx, 'avg' => yyy, 'max' => zzz) */
  595.  
  596.  
  597.     /**
  598.     * Constructor for the Class
  599.     *
  600.     * @access private
  601.     */
  602.     function Net_Ping_Result($result$sysname)
  603.     {
  604.         $this->_raw_data $result;
  605.         $this->_sysname  $sysname;
  606.  
  607.         $this->_parseResult();
  608.     /* function Net_Ping_Result() */
  609.  
  610.     /**
  611.     * Factory for Net_Ping_Result
  612.     *
  613.     * @access public
  614.     * @param array $result Net_Ping result
  615.     * @param string $sysname OS_Guess::sysname
  616.     */
  617.     function factory($result$sysname)
  618.     {
  619.         if (!Net_Ping_Result::_prepareParseResult($sysname)) {
  620.             return PEAR::throwError(NET_PING_RESULT_UNSUPPORTED_BACKEND_MSGNET_PING_RESULT_UNSUPPORTED_BACKEND);
  621.         else {
  622.             return new Net_Ping_Result($result$sysname);
  623.         }
  624.     }  /* function factory() */
  625.  
  626.     /**
  627.     * Preparation method for _parseResult
  628.     *
  629.     * @access private
  630.     * @param string $sysname OS_Guess::sysname
  631.     *  $return bool
  632.     */
  633.     function _prepareParseResult($sysname)
  634.     {
  635.         $parse_methods array_values(array_map('strtolower'get_class_methods('Net_Ping_Result')));
  636.  
  637.         return in_array('_parseresult'.$sysname$parse_methods);
  638.     /* function _prepareParseResult() */
  639.  
  640.     /**
  641.     * Delegates the parsing routine according to $this->_sysname
  642.     *
  643.     * @access private
  644.     */
  645.     function _parseResult()
  646.     {
  647.         call_user_func(array(&$this'_parseResult'.$this->_sysname));
  648.     /* function _parseResult() */
  649.  
  650.     /**
  651.     * Parses the output of Linux' ping command
  652.     *
  653.     * @access private
  654.     * @see _parseResultlinux
  655.     */
  656.     function _parseResultlinux()
  657.     {
  658.         $raw_data_len   count($this->_raw_data);
  659.         $icmp_seq_count $raw_data_len - 4;
  660.  
  661.         /* loop from second elment to the fifths last */
  662.         for($idx = 1; $idx $icmp_seq_count$idx++{
  663.                 $parts explode(' '$this->_raw_data[$idx]);
  664.                 $this->_icmp_sequence[substr(@$parts[4]9strlen(@$parts[4]))substr(@$parts[6]5strlen(@$parts[6]));
  665.             }
  666.         $this->_bytes_per_request $parts[0];
  667.         $this->_bytes_total       = (int)$parts[0$icmp_seq_count;
  668.         $this->_target_ip         substr($parts[3]0-1);
  669.         $this->_ttl               substr($parts[5]4strlen($parts[3]));
  670.  
  671.         $stats explode(','$this->_raw_data[$raw_data_len - 2]);
  672.         $transmitted explode(' '$stats[0]);
  673.         $this->_transmitted $transmitted[0];
  674.  
  675.         $received explode(' '$stats[1]);
  676.         $this->_received $received[1];
  677.  
  678.         $loss explode(' '$stats[2]);
  679.         $this->_loss = (int)$loss[1];
  680.  
  681.         $round_trip explode('/'str_replace('=''/'substr($this->_raw_data[$raw_data_len - 1]0-3)));
  682.  
  683.         /* if mdev field exists, shift input one unit left */
  684.         if (strpos($this->_raw_data[$raw_data_len - 1]'mdev')) {
  685.             /* do not forget the rtt field */
  686.             $this->_round_trip['min']    ltrim($round_trip[5]);
  687.             $this->_round_trip['avg']    $round_trip[6];
  688.             $this->_round_trip['max']    $round_trip[7];
  689.         else {
  690.             $this->_round_trip['min']    ltrim($round_trip[4]);
  691.             $this->_round_trip['avg']    $round_trip[5];
  692.             $this->_round_trip['max']    $round_trip[6];
  693.         }
  694.     /* function _parseResultlinux() */
  695.  
  696.     /**
  697.     * Parses the output of NetBSD's ping command
  698.     *
  699.     * @access private
  700.     * @see _parseResultfreebsd
  701.     */
  702.     function _parseResultnetbsd()
  703.     {
  704.         $this->_parseResultfreebsd();
  705.     /* function _parseResultnetbsd() */
  706.   
  707.     /**
  708.     * Parses the output of Darwin's ping command
  709.     *
  710.     * @access private
  711.     */
  712.     function _parseResultdarwin()
  713.     {
  714.         $raw_data_len   count($this->_raw_data);
  715.         $icmp_seq_count $raw_data_len - 5;
  716.  
  717.         /* loop from second elment to the fifths last */
  718.         for($idx = 1; $idx $icmp_seq_count$idx++{
  719.             $parts explode(' '$this->_raw_data[$idx]);
  720.             $this->_icmp_sequence[substr($parts[4]9strlen($parts[4]))substr($parts[6]5strlen($parts[6]));
  721.         }
  722.  
  723.         $this->_bytes_per_request = (int)$parts[0];
  724.         $this->_bytes_total       = (int)($this->_bytes_per_request $icmp_seq_count);
  725.         $this->_target_ip         substr($parts[3]0-1);
  726.         $this->_ttl               = (int)substr($parts[5]4strlen($parts[3]));
  727.  
  728.         $stats explode(','$this->_raw_data[$raw_data_len - 2]);
  729.         $transmitted explode(' '$stats[0]);
  730.         $this->_transmitted = (int)$transmitted[0];
  731.  
  732.         $received explode(' '$stats[1]);
  733.         $this->_received = (int)$received[1];
  734.  
  735.         $loss explode(' '$stats[2]);
  736.         $this->_loss = (int)$loss[1];
  737.  
  738.         $round_trip explode('/'str_replace('=''/'substr($this->_raw_data[$raw_data_len - 1]0-3)));
  739.  
  740.         $this->_round_trip['min']    = (float)ltrim($round_trip[3]);
  741.         $this->_round_trip['avg']    = (float)$round_trip[4];
  742.         $this->_round_trip['max']    = (float)$round_trip[5];
  743.         $this->_round_trip['stddev'= NULL; /* no stddev */
  744.     /* function _parseResultdarwin() */
  745.  
  746.     /**
  747.     * Parses the output of HP-UX' ping command
  748.     *
  749.     * @access private
  750.     */   
  751.     function _parseResulthpux()
  752.     {
  753.         $parts          = array();
  754.         $raw_data_len   count($this->_raw_data);
  755.         $icmp_seq_count $raw_data_len - 5;
  756.  
  757.         /* loop from second elment to the fifths last */
  758.         for($idx = 1; $idx <= $icmp_seq_count$idx++{
  759.             $parts explode(' '$this->_raw_data[$idx]);
  760.             $this->_icmp_sequence[(int)substr($parts[4]9strlen($parts[4]))= (int)substr($parts[5]5strlen($parts[5]));
  761.         }
  762.         $this->_bytes_per_request = (int)$parts[0];
  763.         $this->_bytes_total       = (int)($parts[0$icmp_seq_count);
  764.         $this->_target_ip         = NULL; /* no target ip */
  765.         $this->_ttl               = NULL; /* no ttl */
  766.  
  767.         $stats explode(','$this->_raw_data[$raw_data_len - 2]);
  768.         $transmitted explode(' '$stats[0]);
  769.         $this->_transmitted = (int)$transmitted[0];
  770.  
  771.         $received explode(' '$stats[1]);
  772.         $this->_received = (int)$received[1];
  773.  
  774.         $loss explode(' '$stats[2]);
  775.         $this->_loss = (int)$loss[1];
  776.  
  777.         $round_trip explode('/'str_replace('=''/',$this->_raw_data[$raw_data_len - 1]));
  778.  
  779.         $this->_round_trip['min']    = (int)ltrim($round_trip[3]);
  780.         $this->_round_trip['avg']    = (int)$round_trip[4];
  781.         $this->_round_trip['max']    = (int)$round_trip[5];
  782.         $this->_round_trip['stddev'= NULL; /* no stddev */
  783.     /* function _parseResulthpux() */
  784.  
  785.     /**
  786.     * Parses the output of AIX' ping command
  787.     *
  788.     * @access private
  789.     */   
  790.     function _parseResultaix()
  791.     {
  792.         $parts          = array();
  793.         $raw_data_len   count($this->_raw_data);
  794.         $icmp_seq_count $raw_data_len - 5;
  795.  
  796.         /* loop from second elment to the fifths last */
  797.         for($idx = 1; $idx <= $icmp_seq_count$idx++{
  798.             $parts explode(' '$this->_raw_data[$idx]);
  799.             $this->_icmp_sequence[(int)substr($parts[4]9strlen($parts[4]))= (int)substr($parts[6]5strlen($parts[6]));
  800.         }
  801.         $this->_bytes_per_request = (int)$parts[0];
  802.         $this->_bytes_total       = (int)($parts[0$icmp_seq_count);
  803.         $this->_target_ip         substr($parts[3]0-1);
  804.         $this->_ttl               = (int)substr($parts[5]4strlen($parts[3]));
  805.  
  806.         $stats explode(','$this->_raw_data[$raw_data_len - 2]);
  807.         $transmitted explode(' '$stats[0]);
  808.         $this->_transmitted = (int)$transmitted[0];
  809.  
  810.         $received explode(' '$stats[1]);
  811.         $this->_received = (int)$received[1];
  812.  
  813.         $loss explode(' '$stats[2]);
  814.         $this->_loss = (int)$loss[1];
  815.  
  816.         $round_trip explode('/'str_replace('=''/',$this->_raw_data[$raw_data_len - 1]));
  817.  
  818.         $this->_round_trip['min']    = (int)ltrim($round_trip[3]);
  819.         $this->_round_trip['avg']    = (int)$round_trip[4];
  820.         $this->_round_trip['max']    = (int)$round_trip[5];
  821.         $this->_round_trip['stddev'= NULL; /* no stddev */
  822.     /* function _parseResultaix() */
  823.  
  824.     /**
  825.     * Parses the output of FreeBSD's ping command
  826.     *
  827.     * @access private
  828.     */
  829.     function _parseResultfreebsd()
  830.     {
  831.         $raw_data_len   count($this->_raw_data);
  832.         $icmp_seq_count $raw_data_len - 5;
  833.  
  834.         /* loop from second elment to the fifths last */
  835.         for($idx = 1; $idx $icmp_seq_count$idx++{
  836.            $parts explode(' '$this->_raw_data[$idx]);
  837.            $this->_icmp_sequence[substr($parts[4]9strlen($parts[4]))substr($parts[6]5strlen($parts[6]));
  838.         }
  839.  
  840.         $this->_bytes_per_request = (int)$parts[0];
  841.         $this->_bytes_total       = (int)($parts[0$icmp_seq_count);
  842.         $this->_target_ip         substr($parts[3]0-1);
  843.         $this->_ttl               = (int)substr($parts[5]4strlen($parts[3]));
  844.  
  845.         $stats explode(','$this->_raw_data[$raw_data_len - 2]);
  846.         $transmitted explode(' '$stats[0]);
  847.         $this->_transmitted = (int)$transmitted[0];
  848.  
  849.         $received explode(' '$stats[1]);
  850.         $this->_received = (int)$received[1];
  851.  
  852.         $loss explode(' '$stats[2]);
  853.         $this->_loss = (int)$loss[1];
  854.  
  855.         $round_trip explode('/'str_replace('=''/'substr($this->_raw_data[$raw_data_len - 1]0-3)));
  856.  
  857.         $this->_round_trip['min']    = (float)ltrim($round_trip[4]);
  858.         $this->_round_trip['avg']    = (float)$round_trip[5];
  859.         $this->_round_trip['max']    = (float)$round_trip[6];
  860.         $this->_round_trip['stddev'= (float)$round_trip[7];
  861.     /* function _parseResultfreebsd() */
  862.  
  863.     /**
  864.     * Parses the output of Windows' ping command
  865.     *
  866.     * @author
  867.     * @access private
  868.     */
  869.     function _parseResultwindows()
  870.     {
  871.         $raw_data_len   count($this->_raw_data);
  872.         $icmp_seq_count $raw_data_len - 8;
  873.  
  874.         /* loop from fourth elment to the sixths last */
  875.         for($idx = 1; $idx <= $icmp_seq_count$idx++{
  876.             $parts explode(' '$this->_raw_data[$idx + 2]);
  877.             $this->_icmp_sequence[$idx - 1= (int)substr(end(split('='$parts[4]))0-2);
  878.  
  879.             $ttl = (int)substr($parts[5]4strlen($parts[3]));
  880.             if ($ttl > 0 && $this->_ttl == 0{
  881.                 $this->_ttl $ttl;
  882.             }
  883.         }
  884.  
  885.        
  886.         $parts explode(' '$this->_raw_data[1]);
  887.         $this->_bytes_per_request = (int)$parts[4];
  888.         $this->_bytes_total       $this->_bytes_per_request $icmp_seq_count;
  889.         $this->_target_ip         substr(trim($parts[2])1-1);
  890.  
  891.         $stats explode(','$this->_raw_data[$raw_data_len - 3]);
  892.         $transmitted explode('='$stats[0]);
  893.         $this->_transmitted = (int)$transmitted[1];
  894.  
  895.         $received explode('='$stats[1]);
  896.         $this->_received = (int)$received[1];
  897.  
  898.         $loss explode('='$stats[2]);
  899.         $this->_loss = (int)$loss[1];
  900.  
  901.         $round_trip explode(','str_replace('='','$this->_raw_data[$raw_data_len - 1]));
  902.         $this->_round_trip['min'= (int)substr(trim($round_trip[1])0-2);
  903.         $this->_round_trip['max'= (int)substr(trim($round_trip[3])0-2);
  904.         $this->_round_trip['avg'= (int)substr(trim($round_trip[5])0-2);
  905.     /* function _parseResultwindows() */
  906.  
  907.     /**
  908.     * Returns a Ping_Result property
  909.     *
  910.     * @param string $name property name
  911.     * @return mixed property value
  912.     * @access public
  913.     */
  914.     function getValue($name)
  915.     {
  916.         return isset($this->$name)?$this->$name:'';
  917.     /* function getValue() */
  918.  
  919.     /**
  920.     * Accessor for $this->_target_ip;
  921.     *
  922.     * @return string IP address
  923.     * @access public
  924.     * @see Ping_Result::_target_ip
  925.     */
  926.     function getTargetIp()
  927.     {
  928.         return $this->_target_ip;
  929.     /* function getTargetIp() */
  930.  
  931.     /**
  932.     * Accessor for $this->_icmp_sequence;
  933.     *
  934.     * @return array ICMP sequence
  935.     * @access private
  936.     * @see Ping_Result::_icmp_sequence
  937.     */
  938.     function getICMPSequence()
  939.     {
  940.         return $this->_icmp_sequence;
  941.     /* function getICMPSequencs() */
  942.  
  943.     /**
  944.     * Accessor for $this->_bytes_per_request;
  945.     *
  946.     * @return int bytes per request
  947.     * @access private
  948.     * @see Ping_Result::_bytes_per_request
  949.     */
  950.     function getBytesPerRequest()
  951.     {
  952.         return $this->_bytes_per_request;
  953.     /* function getBytesPerRequest() */
  954.  
  955.     /**
  956.     * Accessor for $this->_bytes_total;
  957.     *
  958.     * @return int total bytes
  959.     * @access private
  960.     * @see Ping_Result::_bytes_total
  961.     */
  962.     function getBytesTotal()
  963.     {
  964.         return $this->_bytes_total;
  965.     /* function getBytesTotal() */
  966.  
  967.     /**
  968.     * Accessor for $this->_ttl;
  969.     *
  970.     * @return int TTL
  971.     * @access private
  972.     * @see Ping_Result::_ttl
  973.     */
  974.     function getTTL()
  975.     {
  976.         return $this->_ttl;
  977.     /* function getTTL() */
  978.  
  979.     /**
  980.     * Accessor for $this->_raw_data;
  981.     *
  982.     * @return array raw data
  983.     * @access private
  984.     * @see Ping_Result::_raw_data
  985.     */
  986.     function getRawData()
  987.     {
  988.         return $this->_raw_data;
  989.     /* function getRawData() */
  990.  
  991.     /**
  992.     * Accessor for $this->_sysname;
  993.     *
  994.     * @return string OS_Guess::sysname
  995.     * @access private
  996.     * @see Ping_Result::_sysname
  997.     */
  998.     function getSystemName()
  999.     {
  1000.         return $this->_sysname;
  1001.     /* function getSystemName() */
  1002.  
  1003.     /**
  1004.     * Accessor for $this->_round_trip;
  1005.     *
  1006.     * @return array statistical information
  1007.     * @access private
  1008.     * @see Ping_Result::_round_trip
  1009.     */
  1010.     function getRoundTrip()
  1011.     {
  1012.         return $this->_round_trip;
  1013.     /* function getRoundTrip() */
  1014.  
  1015.     /**
  1016.     * Accessor for $this->_round_trip['min'];
  1017.     *
  1018.     * @return array statistical information
  1019.     * @access private
  1020.     * @see Ping_Result::_round_trip
  1021.     */
  1022.     function getMin()
  1023.     {
  1024.         return $this->_round_trip['min'];
  1025.     /* function getMin() */
  1026.  
  1027.     /**
  1028.     * Accessor for $this->_round_trip['max'];
  1029.     *
  1030.     * @return array statistical information
  1031.     * @access private
  1032.     * @see Ping_Result::_round_trip
  1033.     */
  1034.     function getMax()
  1035.     {
  1036.         return $this->_round_trip['max'];
  1037.     /* function getMax() */
  1038.  
  1039.     /**
  1040.     * Accessor for $this->_round_trip['stddev'];
  1041.     *
  1042.     * @return array statistical information
  1043.     * @access private
  1044.     * @see Ping_Result::_round_trip
  1045.     */
  1046.     function getStddev()
  1047.     {
  1048.         return $this->_round_trip['stddev'];
  1049.     /* function getStddev() */
  1050.  
  1051.     /**
  1052.     * Accessor for $this->_round_tripp['avg'];
  1053.     *
  1054.     * @return array statistical information
  1055.     * @access private
  1056.     * @see Ping_Result::_round_trip
  1057.     */
  1058.     function getAvg()
  1059.     {
  1060.         return $this->_round_trip['avg'];
  1061.     /* function getAvg() */
  1062.  
  1063.     /**
  1064.     * Accessor for $this->_transmitted;
  1065.     *
  1066.     * @return array statistical information
  1067.     * @access private
  1068.     */
  1069.     function getTransmitted()
  1070.     {
  1071.         return $this->_transmitted;
  1072.     /* function getTransmitted() */
  1073.  
  1074.     /**
  1075.     * Accessor for $this->_received;
  1076.     *
  1077.     * @return array statistical information
  1078.     * @access private
  1079.     */
  1080.     function getReceived()
  1081.     {
  1082.         return $this->_received;
  1083.     /* function getReceived() */
  1084.  
  1085.     /**
  1086.     * Accessor for $this->_loss;
  1087.     *
  1088.     * @return array statistical information
  1089.     * @access private
  1090.     */
  1091.     function getLoss()
  1092.     {
  1093.         return $this->_loss;
  1094.     /* function getLoss() */
  1095.  
  1096. /* class Net_Ping_Result */
  1097. ?>

Documentation generated on Mon, 11 Mar 2019 14:31:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.