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

Documentation generated on Mon, 11 Mar 2019 15:06:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.