1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Ping for Laravel. |
5
|
|
|
* |
6
|
|
|
* This class makes Ping request to a host. |
7
|
|
|
* |
8
|
|
|
* Ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway. |
9
|
|
|
* |
10
|
|
|
* @author Angel Campos <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @requires PHP 8.0 |
13
|
|
|
* |
14
|
|
|
* @version 2.1.2 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Acamposm\Ping; |
18
|
|
|
|
19
|
|
|
use Acamposm\Ping\Exceptions\PingFailedException; |
20
|
|
|
use Acamposm\Ping\Exceptions\UnknownOSException; |
21
|
|
|
use Acamposm\Ping\Parsers\PingParserForNix; |
22
|
|
|
use Acamposm\Ping\Parsers\PingParserForWindows; |
23
|
|
|
use Exception; |
24
|
|
|
|
25
|
|
|
class Ping |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var PingCommandBuilder |
29
|
|
|
*/ |
30
|
|
|
protected PingCommandBuilder $command; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Timer |
34
|
|
|
*/ |
35
|
|
|
protected Timer $timer; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Ping constructor. |
39
|
|
|
* |
40
|
|
|
* @param PingCommandBuilder $command |
41
|
|
|
*/ |
42
|
|
|
public function __construct(PingCommandBuilder $command) |
43
|
|
|
{ |
44
|
|
|
$this->command = $command; |
45
|
|
|
|
46
|
|
|
$this->timer = new Timer(); |
47
|
|
|
$this->timer->Start(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Parse the ping result and return an object. |
52
|
|
|
* |
53
|
|
|
* @param array $ping |
54
|
|
|
* |
55
|
|
|
* @throws UnknownOSException |
56
|
|
|
* |
57
|
|
|
* @return object |
58
|
|
|
*/ |
59
|
|
|
protected function parse(array $ping): object |
60
|
|
|
{ |
61
|
|
|
if (System::isLinux() || System::isOSX()) { |
62
|
|
|
return (new PingParserForNix($ping))->parse(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (System::isWindows()) { |
66
|
|
|
return (new PingParserForWindows($ping))->parse(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new UnknownOSException(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Remove binary casting from the beginning of the strings. |
74
|
|
|
* |
75
|
|
|
* @param array $ping |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
private function cleanBinaryString(array $ping): array |
80
|
|
|
{ |
81
|
|
|
$cleaned = []; |
82
|
|
|
|
83
|
|
|
foreach ($ping as $row) { |
84
|
|
|
$cleaned[] = preg_replace('/[[:^print:]]/', '', $row); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $cleaned; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @throws Exception |
92
|
|
|
* |
93
|
|
|
* @return object |
94
|
|
|
*/ |
95
|
|
|
public function run(): object |
96
|
|
|
{ |
97
|
|
|
$ping = $this->executePing(); |
98
|
|
|
|
99
|
|
|
// Return the result if lines count are less than three. |
100
|
|
|
if (count($ping) < 3) { |
101
|
|
|
return (object) $ping; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$ping_object = $this->parse($ping); |
105
|
|
|
|
106
|
|
|
$ping_object->options = $this->command->getOptions(); |
107
|
|
|
$ping_object->time = $this->timer->getResults(); |
108
|
|
|
|
109
|
|
|
return $ping_object; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return the result of the execution of ping command. |
114
|
|
|
* |
115
|
|
|
* @throws Exception |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
private function executePing(): array |
120
|
|
|
{ |
121
|
|
|
exec($this->command->get(), $exec_result); |
122
|
|
|
|
123
|
|
|
if (!is_array($exec_result)) { |
124
|
|
|
throw new PingFailedException(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->cleanBinaryString($exec_result); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|