-
Notifications
You must be signed in to change notification settings - Fork 3
/
Ping.php
1225 lines (1095 loc) · 37.8 KB
/
Ping.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 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 Schrder <k.schroeder@php.net> |
// | Craig Constantine <cconstantine@php.net> |
// +----------------------------------------------------------------------+
//
// $Id$
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);
/**
* 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$
* @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::raiseError(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();
// Refine the sysname for different Linux bundles/vendors. (This
// should go away if OS_Guess was ever extended to give vendor
// and vendor-version guesses.)
//
// Bear in mind that $sysname is eventually used to craft a
// method name to figure out which backend gets used to parse
// the ping output. Elsewhere, we'll set $sysname back before
// that.
if ('linux' == $sysname) {
if ( file_exists('/etc/lsb-release')
&& false !== ($release=@file_get_contents('/etc/lsb-release'))
&& preg_match('/gutsy/i', $release)
) {
$sysname = 'linuxredhat9';
}
else if ( file_exists('/etc/debian_version') ) {
$sysname = 'linuxdebian';
}else if (file_exists('/etc/redhat-release')
&& false !== ($release= @file_get_contents('/etc/redhat-release'))
)
{
if (preg_match('/release 8/i', $release)) {
$sysname = 'linuxredhat8';
}elseif (preg_match('/release 9/i', $release)) {
$sysname = 'linuxredhat9';
}
}
}
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::raiseError(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 {
// be certain "which" did what we expect. (ref bug #12791)
if ( is_executable($ping_path) ) {
return $ping_path;
}
else {
return NET_PING_CANT_LOCATE_PING_BINARY;
}
}
}
} /* 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 "openbsd":
$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 "linuxdebian":
$retval['pre'] = $quiet.$count.$ttl.$size.$timeout;
$retval['post'] = "";
$this->_sysname = 'linux'; // undo linux vendor refinement hack
break;
case "linuxredhat8":
$retval['pre'] = $iface.$ttl.$count.$quiet.$size.$deadline;
$retval['post'] = "";
$this->_sysname = 'linux'; // undo linux vendor refinement hack
break;
case "linuxredhat9":
$retval['pre'] = $timeout.$iface.$ttl.$count.$quiet.$size.$deadline;
$retval['post'] = "";
$this->_sysname = 'linux'; // undo linux vendor refinement hack
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']." ".escapeshellarg($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);
}
// 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);
} /* 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
* @return bool True on success or false otherwise
*
*/
function checkHost($host, $severely = true)
{
$matches = array();
$this->setArgs(array("count" => 10,
"size" => 32,
"quiet" => null,
"deadline" => 10
)
);
$res = $this->ping($host);
if (PEAR::isError($res)) {
return false;
}
if ($res->_received == 0) {
return false;
}
if ($res->_received != $res->_transmitted && $severely) {
return false;
}
return true;
} /* function checkHost() */
/**
* Output errors with PHP trigger_error(). You can silence the errors
* with prefixing a "@" sign to the function call: @Net_Ping::ping(..);
*
* @param mixed $error a PEAR error or a string with the error message
* @return bool false
* @access private
* @author Kai Schrder <k.schroeder@php.net>
*/
function _raiseError($error)
{
if (PEAR::isError($error)) {
$error = $error->getMessage();
}
trigger_error($error, E_USER_WARNING);
return false;
} /* function _raiseError() */
/**
* Creates the argument list according to platform differences
*
* @return string Argument line
* @access private
*/
function _initArgRelation()
{
$this->_argRelation["sunos"] = array(
"timeout" => NULL,
"ttl" => "-t",
"count" => " ",
"quiet" => "-q",
"size" => " ",
"iface" => "-i"
);
$this->_argRelation["freebsd"] = array (
"timeout" => "-t",
"ttl" => "-m",
"count" => "-c",
"quiet" => "-q",
"size" => NULL,
"iface" => NULL
);
$this->_argRelation["netbsd"] = array (
"timeout" => "-w",
"iface" => "-I",
"ttl" => "-T",
"count" => "-c",
"quiet" => "-q",
"size" => "-s"
);
$this->_argRelation["openbsd"] = array (
"timeout" => "-w",
"iface" => "-I",
"ttl" => "-t",
"count" => "-c",
"quiet" => "-q",
"size" => "-s"
);
$this->_argRelation["darwin"] = array (
"timeout" => "-t",
"iface" => NULL,
"ttl" => NULL,
"count" => "-c",
"quiet" => "-q",
"size" => NULL
);
$this->_argRelation["linux"] = array (
"timeout" => "-W",
"iface" => NULL,
"ttl" => "-t",
"count" => "-c",
"quiet" => "-q",
"size" => "-s",
"deadline" => "-w"
);
$this->_argRelation["linuxdebian"] = array (
"timeout" => "-W",
"iface" => NULL,
"ttl" => "-t",
"count" => "-c",
"quiet" => "-q",
"size" => "-s",
"deadline" => "-w",
);
$this->_argRelation["linuxredhat8"] = array (
"timeout" => NULL,
"iface" => "-I",
"ttl" => "-t",
"count" => "-c",
"quiet" => "-q",
"size" => "-s",
"deadline" => "-w"
);
$this->_argRelation["linuxredhat9"] = array (
"timeout" => "-W",
"iface" => "-I",
"ttl" => "-t",
"count" => "-c",
"quiet" => "-q",
"size" => "-s",
"deadline" => "-w"
);
$this->_argRelation["windows"] = array (
"timeout" => "-w",
"iface" => NULL,
"ttl" => "-i",
"count" => "-n",
"quiet" => NULL,
"size" => "-l"
);
$this->_argRelation["hpux"] = array (
"timeout" => NULL,
"iface" => NULL,
"ttl" => "-t",
"count" => "-n",
"quiet" => NULL,
"size" => " "
);
$this->_argRelation["aix"] = array (
"timeout" => "-i",
"iface" => NULL,
"ttl" => "-T",
"count" => "-c",
"quiet" => NULL,
"size" => "-s"
);
} /* function _initArgRelation() */
} /* class Net_Ping */
/**
* Container class for Net_Ping results
*
* @author Jan Lehnardt <jan@php.net>
* @version $Revision$
* @package Net
* @access private
*/
class Net_Ping_Result
{
/**
* ICMP sequence number and associated time in ms
*
* @var array
* @access private
*/
var $_icmp_sequence = array(); /* array($sequence_number => $time ) */
/**
* The target's IP Address
*
* @var string
* @access private
*/
var $_target_ip;
/**
* Number of bytes that are sent with each ICMP request
*
* @var int
* @access private
*/
var $_bytes_per_request;
/**
* The total number of bytes that are sent with all ICMP requests
*
* @var int
* @access private
*/
var $_bytes_total;
/**
* The ICMP request's TTL
*
* @var int
* @access private
*/
var $_ttl;
/**
* The raw Net_Ping::result
*
* @var array
* @access private
*/
var $_raw_data = array();
/**
* The Net_Ping::_sysname
*
* @var int
* @access private
*/
var $_sysname;
/**
* Statistical information about the ping
*
* @var int
* @access private
*/
var $_round_trip = array(); /* array('min' => xxx, 'avg' => yyy, 'max' => zzz) */
/**
* Constructor for the Class
*
* @access private
*/
function Net_Ping_Result($result, $sysname)
{
$this->_raw_data = $result;
// The _sysname property is no longer used by Net_Ping_result.
// The property remains for backwards compatibility so the
// getSystemName() method continues to work.
$this->_sysname = $sysname;
$this->_parseResult();
} /* function Net_Ping_Result() */
/**
* Factory for Net_Ping_Result
*
* @access public
* @param array $result Net_Ping result
* @param string $sysname OS_Guess::sysname
*/
function factory($result, $sysname)
{
return new Net_Ping_Result($result, $sysname);
} /* function factory() */
/**
* Parses the raw output from the ping utility.
*
* @access private
*/
function _parseResult()
{
// MAINTAINERS:
//
// If you're in this class fixing or extending the parser
// please add another file in the 'tests/test_parser_data/'
// directory which exemplafies the problem. And of course
// you'll want to run the 'tests/test_parser.php' (which
// contains easy how-to instructions) to make sure you haven't
// broken any existing behaviour.
// operate on a copy of the raw output since we're going to modify it
$data = $this->_raw_data;
// remove leading and trailing blank lines from output
$this->_parseResultTrimLines($data);
// separate the output into upper and lower portions,
// and trim those portions
$this->_parseResultSeparateParts($data, $upper, $lower);
$this->_parseResultTrimLines($upper);
$this->_parseResultTrimLines($lower);
// extract various things from the ping output . . .
$this->_target_ip = $this->_parseResultDetailTargetIp($upper);
$this->_bytes_per_request = $this->_parseResultDetailBytesPerRequest($upper);
$this->_ttl = $this->_parseResultDetailTtl($upper);
$this->_icmp_sequence = $this->_parseResultDetailIcmpSequence($upper);
$this->_round_trip = $this->_parseResultDetailRoundTrip($lower);
$this->_parseResultDetailTransmitted($lower);
$this->_parseResultDetailReceived($lower);
$this->_parseResultDetailLoss($lower);
if ( isset($this->_transmitted) ) {
$this->_bytes_total = $this->_transmitted * $this->_bytes_per_request;
}
} /* function _parseResult() */
/**
* determinces the number of bytes sent by ping per ICMP ECHO
*
* @access private
*/
function _parseResultDetailBytesPerRequest($upper)
{
// The ICMP ECHO REQUEST and REPLY packets should be the same
// size. So we can also find what we want in the output for any
// succesful ICMP reply which ping printed.
for ( $i=1; $i<count($upper); $i++ ) {
// anything like "64 bytes " at the front of any line in $upper??
if ( preg_match('/^\s*(\d+)\s*bytes/i', $upper[$i], $matches) ) {
return( (int)$matches[1] );
}
// anything like "bytes=64" in any line in the buffer??
if ( preg_match('/bytes=(\d+)/i', $upper[$i], $matches) ) {
return( (int)$matches[1] );
}
}
// Some flavors of ping give two numbers, as in "n(m) bytes", on
// the first line. We'll take the first number and add 8 for the
// 8 bytes of header and such in an ICMP ECHO REQUEST.
if ( preg_match('/(\d+)\(\d+\)\D+$/', $upper[0], $matches) ) {
return( (int)(8+$matches[1]) );
}
// Ok we'll just take the rightmost number on the first line. It
// could be "bytes of data" or "whole packet size". But to
// distinguish would require language-specific patterns. Most
// ping flavors just put the number of data (ie, payload) bytes
// if they don't specify both numbers as n(m). So we add 8 bytes
// for the ICMP headers.
if ( preg_match('/(\d+)\D+$/', $upper[0], $matches) ) {
return( (int)(8+$matches[1]) );
}
// then we have no idea...
return( NULL );
}
/**
* determines the round trip time (RTT) in milliseconds for each
* ICMP ECHO which returned. Note that the array is keyed with the
* sequence number of each packet; If any packets are lost, the
* corresponding sequence number will not be found in the array keys.
*
* @access private
*/
function _parseResultDetailIcmpSequence($upper)
{
// There is a great deal of variation in the per-packet output
// from various flavors of ping. There are language variations
// (time=, rtt=, zeit=, etc), field order variations, and some
// don't even generate sequence numbers.
//
// Since our goal is to build an array listing the round trip
// times of each packet, our primary concern is to locate the
// time. The best way seems to be to look for an equals
// character, a number and then 'ms'. All the "time=" versions
// of ping will match this methodology, and all the pings which
// don't show "time=" (that I've seen examples from) also match
// this methodolgy.
$results = array();
for ( $i=1; $i<count($upper); $i++ ) {
// by our definition, it's not a success line if we can't
// find the time
if ( preg_match('/=\s*([\d+\.]+)\s*ms/i', $upper[$i], $matches) ) {
// float cast deals neatly with values like "126." which
// some pings generate
$rtt = (float)$matches[1];
// does the line have an obvious sequence number?
if ( preg_match('/icmp_seq\s*=\s*([\d+]+)/i', $upper[$i], $matches) ) {
$results[$matches[1]] = $rtt;
}
else {
// we use the number of the line as the sequence number
$results[($i-1)] = $rtt;
}
}
}
return( $results );
}
/**
* Locates the "packets lost" percentage in the ping output
*
* @access private
*/
function _parseResultDetailLoss($lower)
{
for ( $i=1; $i<count($lower); $i++ ) {
if ( preg_match('/(\d+)%/', $lower[$i], $matches) ) {
$this->_loss = (int)$matches[1];
return;
}
}
}
/**
* Locates the "packets received" in the ping output
*
* @access private
*/
function _parseResultDetailReceived($lower)
{
for ( $i=1; $i<count($lower); $i++ ) {
// the second number on the line
if ( preg_match('/^\D*\d+\D+(\d+)/', $lower[$i], $matches) ) {
$this->_received = (int)$matches[1];
return;
}
}
}
/**
* determines the mininum, maximum, average and standard deviation
* of the round trip times.
*
* @access private
*/
function _parseResultDetailRoundTrip($lower)
{
// The first pattern will match a sequence of 3 or 4
// alaphabet-char strings separated with slashes without
// presuming the order. eg, "min/max/avg" and
// "min/max/avg/mdev". Some ping flavors don't have the standard
// deviation value, and some have different names for it when
// present.
$p1 = '[a-z]+/[a-z]+/[a-z]+/?[a-z]*';
// And the pattern for 3 or 4 numbers (decimal values permitted)
// separated by slashes.
$p2 = '[0-9\.]+/[0-9\.]+/[0-9\.]+/?[0-9\.]*';
$results = array();
$matches = array();
for ( $i=(count($lower)-1); $i>=0; $i-- ) {
if ( preg_match('|('.$p1.')[^0-9]+('.$p2.')|i', $lower[$i], $matches) ) {
break;
}
}
// matches?
if ( count($matches) > 0 ) {
// we want standardized keys in the array we return. Here we
// look for the values (min, max, etc) and setup the return
// hash
$fields = explode('/', $matches[1]);
$values = explode('/', $matches[2]);
for ( $i=0; $i<count($fields); $i++ ) {
if ( preg_match('/min/i', $fields[$i]) ) {
$results['min'] = (float)$values[$i];
}
else if ( preg_match('/max/i', $fields[$i]) ) {
$results['max'] = (float)$values[$i];
}
else if ( preg_match('/avg/i', $fields[$i]) ) {
$results['avg'] = (float)$values[$i];
}
else if ( preg_match('/dev/i', $fields[$i]) ) { # stddev or mdev
$results['stddev'] = (float)$values[$i];
}
}
return( $results );
}
// So we had no luck finding RTT info in a/b/c layout. Some ping
// flavors give the RTT information in an "a=1 b=2 c=3" sort of
// layout.
$p3 = '[a-z]+\s*=\s*([0-9\.]+).*';
for ( $i=(count($lower)-1); $i>=0; $i-- ) {
if ( preg_match('/min.*max/i', $lower[$i]) ) {
if ( preg_match('/'.$p3.$p3.$p3.'/i', $lower[$i], $matches) ) {
$results['min'] = $matches[1];
$results['max'] = $matches[2];
$results['avg'] = $matches[3];
}
break;
}
}
// either an array of min, max and avg from just above, or still
// the empty array from initialization way above
return( $results );
}
/**
* determinces the target IP address actually used by ping
*
* @access private
*/
function _parseResultDetailTargetIp($upper)
{
// Grab the first IP addr we can find. Most ping flavors
// put the target IP on the first line, but some only list it
// in successful ping packet lines.
for ( $i=0; $i<count($upper); $i++ ) {
if ( preg_match('/(\d+\.\d+\.\d+\.\d+)/', $upper[$i], $matches) ) {
return( $matches[0] );
}
}
// no idea...
return( NULL );
}
/**
* Locates the "packets received" in the ping output
*
* @access private
*/
function _parseResultDetailTransmitted($lower)
{
for ( $i=1; $i<count($lower); $i++ ) {
// the first number on the line
if ( preg_match('/^\D*(\d+)/', $lower[$i], $matches) ) {
$this->_transmitted = (int)$matches[1];
return;
}
}
}
/**
* determinces the time to live (TTL) actually used by ping
*
* @access private
*/
function _parseResultDetailTtl($upper)
{
//extract TTL from first icmp echo line
for ( $i=1; $i<count($upper); $i++ ) {
if ( preg_match('/ttl=(\d+)/i', $upper[$i], $matches)
&& (int)$matches[1] > 0
) {
return( (int)$matches[1] );
}
}
// No idea what ttl was used. Probably because no packets
// received in reply.
return( NULL );
}
/**
* Modifies the array to temoves leading and trailing blank lines
*
* @access private
*/
function _parseResultTrimLines(&$data)
{
if ( !is_array($data) ) {
print_r($this);
exit;
}
// Trim empty elements from the front
while ( preg_match('/^\s*$/', $data[0]) ) {
array_splice($data, 0, 1);
}
// Trim empty elements from the back
while ( preg_match('/^\s*$/', $data[(count($data)-1)]) ) {
array_splice($data, -1, 1);
}
}
/**
* Separates the upper portion (data about individual ICMP ECHO
* packets) and the lower portion (statistics about the ping
* execution as a whole.)