-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPing.php
executable file
·123 lines (93 loc) · 3.2 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
<?php
namespace dbarr;
use \BadMethodCallException;
use \InvalidArgumentException;
use \Closure;
class Ping {
private static $SUPPORTED_OPERATING_SYSTEMS = [
'freebsd',
];
protected $_binaryCmd;
protected $_cmdOptions = [
'count' => '',
'dfbit' => '',
'timeout' => '',
'packetsize' => '',
];
protected $_defaultOptions = [
];
protected $_parsers = [
];
static public final function getInstance($os) {
if (array_search($os, self::getSupportedOperatingSystems()) === false) {
throw new BadMethodCallException('Unsupported operating system');
}
$class = sprintf('%s\\%s', __CLASS__, $os);
return new $class();
}
static public final function getSupportedOperatingSystems() {
return self::$SUPPORTED_OPERATING_SYSTEMS;
}
public function clearDefaultOptions() {
$this->_defaultOptions = [];
}
public function setDefaultOptions($opts=[]) {
foreach ($opts as $opt => $optval) {
if (array_key_exists($opt, $this->_cmdOptions) === false) {
throw new InvalidArgumentException(sprintf('Unknown option: %s', $opt));
}
}
$this->_defaultOptions = array_merge($this->_defaultOptions, $opts);
}
private function generateCmd($host, $opts=[]) {
$cmd = [escapeshellcmd($this->_binaryCmd)];
$opts = array_merge($this->_defaultOptions, $opts);
foreach ($opts as $opt => $optval) {
if (array_key_exists($opt, $this->_cmdOptions) === false) {
throw new InvalidArgumentException(sprintf('Unknown option: %s', $opt));
}
$option = $this->_cmdOptions[$opt];
$cmd[] = escapeshellarg(call_user_func_array('sprintf', array_merge([$option], is_array($optval) ? $optval : [$optval])));
}
$cmd[] = escapeshellarg($host);
$cmd[] = '2>&1';
$cmd = implode(' ', $cmd);
return $cmd;
}
public function ping($host, $opts=[]) {
$cmd = $this->generateCmd($host, $opts);
$out = [];
$returncode = 0;
exec($cmd, $out, $returncode);
$ret = ['host' => $host];
/*
* 0: response received
* 2: no responses received
* any other value: error occurred
*/
if ($returncode > 0 && $returncode != 2) {
$ret['error'] = implode("\n", $out);
return $ret;
}
$ret['output'] = implode("\n", $out);
foreach ($this->_parsers as $parser_callback) {
$callback_data = $parser_callback($out);
if (is_array($callback_data)) {
$ret = array_merge($ret, $callback_data);
}
}
return $ret;
}
protected function setBinaryCmd($cmd) {
$this->_binaryCmd = $cmd;
}
protected function setCmdOptions(array $opts) {
$this->_cmdOptions = array_merge($this->_cmdOptions, $opts);
}
protected function registerParser($callback) {
if (!is_callable($callback)) {
throw new InvalidArgumentException('Invalid callback');
}
$this->_parsers[] = $callback;
}
}