Ping.php
33.1 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Martin Jansen <mj@php.net> |
// | Tomas V.V.Cox <cox@idecnet.com> |
// | Jan Lehnardt <jan@php.net> |
// | Kai Schröder <k.schroeder@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);
/**************************TODO*******************************************/
/*
*
* - add Net_Ping_Result parser for:
* + IRIX64
* + OSF1
* + BSD/OS
* + OpenBSD
* - fix Net_Ping::checkHost()
* - reset result buffer
*/
/**
* Wrapper class for ping calls
*
* Usage:
*
* <?php
* require_once "Net/Ping.php";
* $ping = Net_Ping::factory();
* if(PEAR::isError($ping)) {
* echo $ping->getMessage();
* } else {
* $ping->setArgs(array('count' => 2));
* var_dump($ping->ping('example.com'));
* }
* ?>
*
* @author Jan Lehnardt <jan@php.net>
* @version $Revision$
* @package Net
* @access public
*/
class Net_Ping
{
/**
* Location where the ping program is stored
*
* @var string
* @access private
*/
var $_ping_path = "";
/**
* Array with the result from the ping execution
*
* @var array
* @access private
*/
var $_result = array();
/**
* OS_Guess instance
*
* @var object
* @access private
*/
var $_OS_Guess = "";
/**
* OS_Guess->getSysname result
*
* @var string
* @access private
*/
var $_sysname = "";
/**
* Ping command arguments
*
* @var array
* @access private
*/
var $_args = array();
/**
* Indicates if an empty array was given to setArgs
*
* @var boolean
* @access private
*/
var $_noArgs = true;
/**
* Contains the argument->option relation
*
* @var array
* @access private
*/
var $_argRelation = array();
/**
* Constructor for the Class
*
* @access private
*/
function Net_Ping($ping_path, $sysname)
{
$this->_ping_path = $ping_path;
$this->_sysname = $sysname;
$this->_initArgRelation();
} /* function Net_Ping() */
/**
* Factory for Net_Ping
*
* @access public
*/
function factory()
{
$ping_path = '';
$sysname = Net_Ping::_setSystemName();
if (($ping_path = Net_Ping::_setPingPath($sysname)) == NET_PING_CANT_LOCATE_PING_BINARY) {
return PEAR::throwError(NET_PING_CANT_LOCATE_PING_BINARY_MSG, NET_PING_CANT_LOCATE_PING_BINARY);
} else {
return new Net_Ping($ping_path, $sysname);
}
} /* function factory() */
/**
* Resolve the system name
*
* @access private
*/
function _setSystemName()
{
$OS_Guess = new OS_Guess;
$sysname = $OS_Guess->getSysname();
/* Nasty hack for Debian, as it uses a custom ping version */
if ('linux' == $sysname) {
if (file_exists('/etc/debian_version')) {
$sysname = 'debian';
}
}
return $sysname;
} /* function _setSystemName */
/**
* Set the arguments array
*
* @param array $args Hash with options
* @return mixed true or PEAR_error
* @access public
*/
function setArgs($args)
{
if (!is_array($args)) {
return PEAR::throwError(NET_PING_INVALID_ARGUMENTS_MSG, NET_PING_INVALID_ARGUMENTS);
}
$this->_setNoArgs($args);
$this->_args = $args;
return true;
} /* function setArgs() */
/**
* Set the noArgs flag
*
* @param array $args Hash with options
* @return void
* @access private
*/
function _setNoArgs($args)
{
if (0 == count($args)) {
$this->_noArgs = true;
} else {
$this->_noArgs = false;
}
} /* function _setNoArgs() */
/**
* Sets the system's path to the ping binary
*
* @access private
*/
function _setPingPath($sysname)
{
$status = '';
$output = array();
$ping_path = '';
if ("windows" == $sysname) {
return "ping";
} else {
$ping_path = exec("which ping", $output, $status);
if (0 != $status) {
return NET_PING_CANT_LOCATE_PING_BINARY;
} else {
return $ping_path;
}
}
} /* function _setPingPath() */
/**
* Creates the argument list according to platform differences
*
* @return string Argument line
* @access private
*/
function _createArgList()
{
$retval = array();
$timeout = "";
$iface = "";
$ttl = "";
$count = "";
$quiet = "";
$size = "";
$seq = "";
$deadline = "";
foreach($this->_args AS $option => $value) {
if(!empty($option) && isset($this->_argRelation[$this->_sysname][$option]) && NULL != $this->_argRelation[$this->_sysname][$option]) {
${$option} = $this->_argRelation[$this->_sysname][$option]." ".$value." ";
}
}
switch($this->_sysname) {
case "sunos":
if ($size || $count || $iface) {
/* $size and $count must be _both_ defined */
$seq = " -s ";
if ($size == "") {
$size = " 56 ";
}
if ($count == "") {
$count = " 5 ";
}
}
$retval['pre'] = $iface.$seq.$ttl;
$retval['post'] = $size.$count;
break;
case "freebsd":
$retval['pre'] = $quiet.$count.$ttl.$timeout;
$retval['post'] = "";
break;
case "darwin":
$retval['pre'] = $count.$timeout.$size;
$retval['post'] = "";
break;
case "netbsd":
$retval['pre'] = $quiet.$count.$iface.$size.$ttl.$timeout;
$retval['post'] = "";
break;
case "linux":
$retval['pre'] = $quiet.$deadline.$count.$ttl.$size.$timeout;
$retval['post'] = "";
break;
case "debian":
$retval['pre'] = $quiet.$count.$ttl.$size.$timeout;
$retval['post'] = "";
/* undo nasty debian hack*/
$this->_sysname = 'linux';
break;
case "windows":
$retval['pre'] = $count.$ttl.$timeout;
$retval['post'] = "";
break;
case "hpux":
$retval['pre'] = $ttl;
$retval['post'] = $size.$count;
break;
case "aix":
$retval['pre'] = $count.$timeout.$ttl.$size;
$retval['post'] = "";
break;
default:
$retval['pre'] = "";
$retval['post'] = "";
break;
}
return($retval);
} /* function _createArgList() */
/**
* Execute ping
*
* @param string $host hostname
* @return mixed String on error or array with the result
* @access public
*/
function ping($host)
{
if($this->_noArgs) {
$this->setArgs(array('count' => 3));
}
$argList = $this->_createArgList();
$cmd = $this->_ping_path." ".$argList['pre']." ".$host." ".$argList['post'];
exec($cmd, $this->_result);
if (!is_array($this->_result)) {
return PEAR::throwError(NET_PING_FAILED_MSG, NET_PING_FAILED);
}
if (count($this->_result) == 0) {
return PEAR::throwError(NET_PING_HOST_NOT_FOUND_MSG, NET_PING_HOST_NOT_FOUND);
} else {
return Net_Ping_Result::factory($this->_result, $this->_sysname);
}
} /* function ping() */
/**
* Check if a host is up by pinging it
*
* @param string $host The host to test
* @param bool $severely If some of the packages did reach the host
* and severely is false the function will return true
* @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 (!preg_match_all('|\d+|', $res[3], $matches) || count($matches[0]) < 3) {
ob_start();
$rep = ob_get_contents();
ob_end_clean();
trigger_error("Output format seems not to be supported, please report ".
"the following to pear-dev@lists.php.net, including your ".
"version of ping:\n $rep");
return false;
}
if ($matches[0][1] == 0) {
return false;
}
// [0] => transmitted, [1] => received
if ($matches[0][0] != $matches[0][1] && $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 Schröder <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" => "-t",
"iface" => NULL,
"ttl" => "-m",
"count" => "-c",
"quiet" => "-q",
"size" => "-s",
"deadline" => "-w"
);
$this->_argRelation["debian"] = array (
"timeout" => "-t",
"iface" => NULL,
"ttl" => "-m",
"count" => "-c",
"quiet" => "-q",
"size" => "-s",
);
$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;
$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)
{
if (!Net_Ping_Result::_prepareParseResult($sysname)) {
return PEAR::throwError(NET_PING_RESULT_UNSUPPORTED_BACKEND_MSG, NET_PING_RESULT_UNSUPPORTED_BACKEND);
} else {
return new Net_Ping_Result($result, $sysname);
}
} /* function factory() */
/**
* Preparation method for _parseResult
*
* @access private
* @param string $sysname OS_Guess::sysname
* $return bool
*/
function _prepareParseResult($sysname)
{
$parse_methods = array_values(get_class_methods('Net_Ping_Result'));
return in_array('_parseresult'.$sysname, $parse_methods);
} /* function _prepareParseResult() */
/**
* Delegates the parsing routine according to $this->_sysname
*
* @access private
*/
function _parseResult()
{
call_user_func(array(&$this, '_parseResult'.$this->_sysname));
} /* function _parseResult() */
/**
* Parses the output of Linux' ping command
*
* @access private
* @see _parseResultlinux
*/
function _parseResultlinux()
{
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 4;
/* loop from second elment to the fifths last */
for($idx = 1; $idx < $icmp_seq_count; $idx++) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_icmp_sequence[substr(@$parts[4], 9, strlen(@$parts[4]))] = substr(@$parts[6], 5, strlen(@$parts[6]));
}
$this->_bytes_per_request = $parts[0];
$this->_bytes_total = (int)$parts[0] * $icmp_seq_count;
$this->_target_ip = substr($parts[3], 0, -1);
$this->_ttl = substr($parts[5], 4, strlen($parts[3]));
$stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
$transmitted = explode(' ', $stats[0]);
$this->_transmitted = $transmitted[0];
$received = explode(' ', $stats[1]);
$this->_received = $received[1];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
/* if mdev field exists, shift input one unit left */
if (strpos($this->_raw_data[$raw_data_len - 1], 'mdev')) {
/* do not forget the rtt field */
$this->_round_trip['min'] = ltrim($round_trip[5]);
$this->_round_trip['avg'] = $round_trip[6];
$this->_round_trip['max'] = $round_trip[7];
} else {
$this->_round_trip['min'] = ltrim($round_trip[4]);
$this->_round_trip['avg'] = $round_trip[5];
$this->_round_trip['max'] = $round_trip[6];
}
} /* function _parseResultlinux() */
/**
* Parses the output of NetBSD's ping command
*
* @access private
* @see _parseResultfreebsd
*/
function _parseResultnetbsd()
{
$this->_parseResultfreebsd();
} /* function _parseResultnetbsd() */
/**
* Parses the output of Darwin's ping command
*
* @access private
*/
function _parseResultdarwin()
{
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 5;
/* loop from second elment to the fifths last */
for($idx = 1; $idx < $icmp_seq_count; $idx++) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_icmp_sequence[substr($parts[4], 9, strlen($parts[4]))] = substr($parts[6], 5, strlen($parts[6]));
}
$this->_bytes_per_request = (int)$parts[0];
$this->_bytes_total = (int)($this->_bytes_per_request * $icmp_seq_count);
$this->_target_ip = substr($parts[3], 0, -1);
$this->_ttl = (int)substr($parts[5], 4, strlen($parts[3]));
$stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
$transmitted = explode(' ', $stats[0]);
$this->_transmitted = (int)$transmitted[0];
$received = explode(' ', $stats[1]);
$this->_received = (int)$received[1];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
$this->_round_trip['min'] = (float)ltrim($round_trip[3]);
$this->_round_trip['avg'] = (float)$round_trip[4];
$this->_round_trip['max'] = (float)$round_trip[5];
$this->_round_trip['stddev'] = NULL; /* no stddev */
} /* function _parseResultdarwin() */
/**
* Parses the output of HP-UX' ping command
*
* @access private
*/
function _parseResulthpux()
{
$parts = array();
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 5;
/* loop from second elment to the fifths last */
for($idx = 1; $idx <= $icmp_seq_count; $idx++) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_icmp_sequence[(int)substr($parts[4], 9, strlen($parts[4]))] = (int)substr($parts[5], 5, strlen($parts[5]));
}
$this->_bytes_per_request = (int)$parts[0];
$this->_bytes_total = (int)($parts[0] * $icmp_seq_count);
$this->_target_ip = NULL; /* no target ip */
$this->_ttl = NULL; /* no ttl */
$stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
$transmitted = explode(' ', $stats[0]);
$this->_transmitted = (int)$transmitted[0];
$received = explode(' ', $stats[1]);
$this->_received = (int)$received[1];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/',$this->_raw_data[$raw_data_len - 1]));
$this->_round_trip['min'] = (int)ltrim($round_trip[3]);
$this->_round_trip['avg'] = (int)$round_trip[4];
$this->_round_trip['max'] = (int)$round_trip[5];
$this->_round_trip['stddev'] = NULL; /* no stddev */
} /* function _parseResulthpux() */
/**
* Parses the output of AIX' ping command
*
* @access private
*/
function _parseResultaix()
{
$parts = array();
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 5;
/* loop from second elment to the fifths last */
for($idx = 1; $idx <= $icmp_seq_count; $idx++) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_icmp_sequence[(int)substr($parts[4], 9, strlen($parts[4]))] = (int)substr($parts[6], 5, strlen($parts[6]));
}
$this->_bytes_per_request = (int)$parts[0];
$this->_bytes_total = (int)($parts[0] * $icmp_seq_count);
$this->_target_ip = substr($parts[3], 0, -1);
$this->_ttl = (int)substr($parts[5], 4, strlen($parts[3]));
$stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
$transmitted = explode(' ', $stats[0]);
$this->_transmitted = (int)$transmitted[0];
$received = explode(' ', $stats[1]);
$this->_received = (int)$received[1];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/',$this->_raw_data[$raw_data_len - 1]));
$this->_round_trip['min'] = (int)ltrim($round_trip[3]);
$this->_round_trip['avg'] = (int)$round_trip[4];
$this->_round_trip['max'] = (int)$round_trip[5];
$this->_round_trip['stddev'] = NULL; /* no stddev */
} /* function _parseResultaix() */
/**
* Parses the output of FreeBSD's ping command
*
* @access private
*/
function _parseResultfreebsd()
{
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 5;
/* loop from second elment to the fifths last */
for($idx = 1; $idx < $icmp_seq_count; $idx++) {
$parts = explode(' ', $this->_raw_data[$idx]);
$this->_icmp_sequence[substr($parts[4], 9, strlen($parts[4]))] = substr($parts[6], 5, strlen($parts[6]));
}
$this->_bytes_per_request = (int)$parts[0];
$this->_bytes_total = (int)($parts[0] * $icmp_seq_count);
$this->_target_ip = substr($parts[3], 0, -1);
$this->_ttl = (int)substr($parts[5], 4, strlen($parts[3]));
$stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
$transmitted = explode(' ', $stats[0]);
$this->_transmitted = (int)$transmitted[0];
$received = explode(' ', $stats[1]);
$this->_received = (int)$received[1];
$loss = explode(' ', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
$this->_round_trip['min'] = (float)ltrim($round_trip[4]);
$this->_round_trip['avg'] = (float)$round_trip[5];
$this->_round_trip['max'] = (float)$round_trip[6];
$this->_round_trip['stddev'] = (float)$round_trip[7];
} /* function _parseResultfreebsd() */
/**
* Parses the output of Windows' ping command
*
* @author Kai Schröder <k.schroeder@php.net>
* @access private
*/
function _parseResultwindows()
{
$raw_data_len = count($this->_raw_data);
$icmp_seq_count = $raw_data_len - 8;
/* loop from fourth elment to the sixths last */
for($idx = 1; $idx <= $icmp_seq_count; $idx++) {
$parts = explode(' ', $this->_raw_data[$idx + 2]);
$this->_icmp_sequence[$idx - 1] = (int)substr(end(split('=', $parts[4])), 0, -2);
$ttl = (int)substr($parts[5], 4, strlen($parts[3]));
if ($ttl > 0 && $this->_ttl == 0) {
$this->_ttl = $ttl;
}
}
$parts = explode(' ', $this->_raw_data[1]);
$this->_bytes_per_request = (int)$parts[4];
$this->_bytes_total = $this->_bytes_per_request * $icmp_seq_count;
$this->_target_ip = substr(trim($parts[2]), 1, -1);
$stats = explode(',', $this->_raw_data[$raw_data_len - 3]);
$transmitted = explode('=', $stats[0]);
$this->_transmitted = (int)$transmitted[1];
$received = explode('=', $stats[1]);
$this->_received = (int)$received[1];
$loss = explode('=', $stats[2]);
$this->_loss = (int)$loss[1];
$round_trip = explode(',', str_replace('=', ',', $this->_raw_data[$raw_data_len - 1]));
$this->_round_trip['min'] = (int)substr(trim($round_trip[1]), 0, -2);
$this->_round_trip['avg'] = (int)substr(trim($round_trip[3]), 0, -2);
$this->_round_trip['max'] = (int)substr(trim($round_trip[5]), 0, -2);
} /* function _parseResultwindows() */
/**
* Returns a Ping_Result property
*
* @param string $name property name
* @return mixed property value
* @access public
*/
function getValue($name)
{
return isset($this->$name)?$this->$name:'';
} /* function getValue() */
/**
* Accessor for $this->_target_ip;
*
* @return string IP address
* @access public
* @see Ping_Result::_target_ip
*/
function getTargetIp()
{
return $this->_target_ip;
} /* function getTargetIp() */
/**
* Accessor for $this->_icmp_sequence;
*
* @return array ICMP sequence
* @access private
* @see Ping_Result::_icmp_sequence
*/
function getICMPSequence()
{
return $this->_icmp_sequence;
} /* function getICMPSequencs() */
/**
* Accessor for $this->_bytes_per_request;
*
* @return int bytes per request
* @access private
* @see Ping_Result::_bytes_per_request
*/
function getBytesPerRequest()
{
return $this->_bytes_per_request;
} /* function getBytesPerRequest() */
/**
* Accessor for $this->_bytes_total;
*
* @return int total bytes
* @access private
* @see Ping_Result::_bytes_total
*/
function getBytesTotal()
{
return $this->_bytes_total;
} /* function getBytesTotal() */
/**
* Accessor for $this->_ttl;
*
* @return int TTL
* @access private
* @see Ping_Result::_ttl
*/
function getTTL()
{
return $this->_ttl;
} /* function getTTL() */
/**
* Accessor for $this->_raw_data;
*
* @return array raw data
* @access private
* @see Ping_Result::_raw_data
*/
function getRawData()
{
return $this->_raw_data;
} /* function getRawData() */
/**
* Accessor for $this->_sysname;
*
* @return string OS_Guess::sysname
* @access private
* @see Ping_Result::_sysname
*/
function getSystemName()
{
return $this->_sysname;
} /* function getSystemName() */
/**
* Accessor for $this->_round_trip;
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getRoundTrip()
{
return $this->_round_trip;
} /* function getRoundTrip() */
/**
* Accessor for $this->_round_trip['min'];
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getMin()
{
return $this->_round_trip['min'];
} /* function getMin() */
/**
* Accessor for $this->_round_trip['max'];
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getMax()
{
return $this->_round_trip['max'];
} /* function getMax() */
/**
* Accessor for $this->_round_trip['stddev'];
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getStddev()
{
return $this->_round_trip['stddev'];
} /* function getStddev() */
/**
* Accessor for $this->_round_tripp['avg'];
*
* @return array statistical information
* @access private
* @see Ping_Result::_round_trip
*/
function getAvg()
{
return $this->_round_trip['avg'];
} /* function getAvg() */
/**
* Accessor for $this->_transmitted;
*
* @return array statistical information
* @access private
*/
function getTransmitted()
{
return $this->_transmitted;
} /* function getTransmitted() */
/**
* Accessor for $this->_received;
*
* @return array statistical information
* @access private
*/
function getReceived()
{
return $this->_received;
} /* function getReceived() */
/**
* Accessor for $this->_loss;
*
* @return array statistical information
* @access private
*/
function getLoss()
{
return $this->_loss;
} /* function getLoss() */
} /* class Net_Ping_Result */
?>