|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of graze/telnet-client. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @license https://github.com/graze/telnet-client/blob/master/LICENSE |
|
12
|
|
|
* @link https://github.com/graze/telnet-client |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Graze\TelnetClient; |
|
16
|
|
|
|
|
17
|
|
|
use \Graze\TelnetClient\TelnetClientInterface; |
|
18
|
|
|
use \Graze\TelnetClient\PromptMatcher; |
|
19
|
|
|
use \Graze\TelnetClient\InterpretAsCommand; |
|
20
|
|
|
use \Socket\Raw\Socket; |
|
21
|
|
|
use \Socket\Raw\Factory as SocketFactory; |
|
22
|
|
|
use \Graze\TelnetClient\TelnetClientBuilder; |
|
23
|
|
|
use \Exception; |
|
24
|
|
|
use \Graze\TelnetClient\Exception\TelnetException; |
|
25
|
|
|
|
|
26
|
|
|
class TelnetClient implements TelnetClientInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var SocketFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $socketFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var PromptMatcher |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $promptMatcher; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var InterpretAsCommand |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $interpretAsCommand; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $prompt = '\$'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $promptError = 'ERROR'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $lineEnding = "\n"; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var Socket |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $socket; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $buffer; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $NULL; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $DC1; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $IAC; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param SocketFactory $socketFactory |
|
85
|
|
|
* @param PromptMatcher $promptMatcher |
|
86
|
|
|
* @param InterpretAsCommand $interpretAsCommand |
|
87
|
|
|
*/ |
|
88
|
10 |
|
public function __construct( |
|
89
|
|
|
SocketFactory $socketFactory, |
|
90
|
|
|
PromptMatcher $promptMatcher, |
|
91
|
|
|
InterpretAsCommand $interpretAsCommand |
|
92
|
|
|
) { |
|
93
|
10 |
|
$this->socketFactory = $socketFactory; |
|
94
|
10 |
|
$this->promptMatcher = $promptMatcher; |
|
95
|
10 |
|
$this->interpretAsCommand = $interpretAsCommand; |
|
96
|
|
|
|
|
97
|
10 |
|
$this->NULL = chr(0); |
|
98
|
10 |
|
$this->DC1 = chr(17); |
|
99
|
10 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $dsn |
|
103
|
|
|
* @param string $prompt |
|
104
|
|
|
* @param string $promptError |
|
105
|
|
|
* @param string $lineEnding |
|
106
|
|
|
* |
|
107
|
|
|
* @throws TelnetExceptionInterface |
|
108
|
|
|
*/ |
|
109
|
9 |
|
public function connect($dsn, $prompt = null, $promptError = null, $lineEnding = null) |
|
110
|
|
|
{ |
|
111
|
9 |
|
if ($prompt !== null) { |
|
112
|
2 |
|
$this->setPrompt($prompt); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
9 |
|
if ($promptError !== null) { |
|
116
|
2 |
|
$this->setPromptError($promptError); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
9 |
|
if ($lineEnding !== null) { |
|
120
|
8 |
|
$this->setLineEnding($lineEnding); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
try { |
|
124
|
9 |
|
$socket = $this->socketFactory->createClient($dsn); |
|
125
|
|
|
} catch (Exception $e) { |
|
126
|
|
|
throw new TelnetException(sprintf('unable to create socket connection to [%s]', $dsn), 0, $e); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
9 |
|
$this->setSocket($socket); |
|
130
|
9 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $prompt |
|
134
|
|
|
*/ |
|
135
|
1 |
|
public function setPrompt($prompt) |
|
136
|
|
|
{ |
|
137
|
1 |
|
$this->prompt = $prompt; |
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $promptError |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function setPromptError($promptError) |
|
144
|
|
|
{ |
|
145
|
1 |
|
$this->promptError = $promptError; |
|
146
|
1 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $lineEnding |
|
150
|
|
|
*/ |
|
151
|
7 |
|
public function setLineEnding($lineEnding) |
|
152
|
|
|
{ |
|
153
|
7 |
|
$this->lineEnding = $lineEnding; |
|
154
|
7 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param Socket $socket |
|
158
|
|
|
*/ |
|
159
|
9 |
|
public function setSocket(Socket $socket) |
|
160
|
|
|
{ |
|
161
|
9 |
|
$this->socket = $socket; |
|
162
|
9 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $command |
|
166
|
|
|
* @param string $prompt |
|
167
|
|
|
* |
|
168
|
|
|
* @return \Graze\TelnetClient\TelnetResponseInterface |
|
169
|
|
|
*/ |
|
170
|
10 |
|
public function execute($command, $prompt = null) |
|
171
|
|
|
{ |
|
172
|
10 |
|
if (!$this->socket) { |
|
173
|
1 |
|
throw new TelnetException('attempt to execute without a connection - call connect first'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
9 |
|
$this->write($command); |
|
177
|
8 |
|
return $this->getResponse($prompt); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $command |
|
182
|
|
|
* |
|
183
|
|
|
* @return void |
|
184
|
|
|
* @throws TelnetExceptionInterface |
|
185
|
|
|
*/ |
|
186
|
9 |
|
protected function write($command) |
|
187
|
|
|
{ |
|
188
|
|
|
try { |
|
189
|
9 |
|
$this->socket->write($command . $this->lineEnding); |
|
190
|
1 |
|
} catch (Exception $e) { |
|
191
|
1 |
|
throw new TelnetException(sprintf('failed writing to socket [%s]', $command), 0, $e); |
|
192
|
|
|
} |
|
193
|
8 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param string $prompt |
|
197
|
|
|
* |
|
198
|
|
|
* @return \Graze\TelnetClient\TelnetResponseInterface |
|
199
|
|
|
* @throws TelnetExceptionInterface |
|
200
|
|
|
*/ |
|
201
|
8 |
|
protected function getResponse($prompt = null) |
|
202
|
|
|
{ |
|
203
|
8 |
|
$isError = false; |
|
204
|
8 |
|
$buffer = ''; |
|
205
|
|
|
do { |
|
206
|
|
|
// process one character at a time |
|
207
|
|
|
try { |
|
208
|
8 |
|
$character = $this->socket->read(1); |
|
209
|
1 |
|
} catch (Exception $e) { |
|
210
|
1 |
|
throw new TelnetException('failed reading from socket', 0, $e); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
7 |
|
if (in_array($character, [$this->NULL, $this->DC1])) { |
|
214
|
|
|
break; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
7 |
|
if ($this->interpretAsCommand->interpret($character, $this->socket)) { |
|
218
|
|
|
continue; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
7 |
|
$buffer .= $character; |
|
222
|
|
|
|
|
223
|
|
|
// check for prompt |
|
224
|
7 |
|
if ($this->promptMatcher->isMatch($prompt ?: $this->prompt, $buffer, $this->lineEnding)) { |
|
225
|
4 |
|
break; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
// check for error prompt |
|
229
|
7 |
|
if ($this->promptMatcher->isMatch($this->promptError, $buffer, $this->lineEnding)) { |
|
230
|
3 |
|
$isError = true; |
|
231
|
3 |
|
break; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
7 |
|
} while (true); |
|
235
|
|
|
|
|
236
|
7 |
|
return new TelnetResponse( |
|
237
|
|
|
$isError, |
|
238
|
7 |
|
$this->promptMatcher->getResponseText(), |
|
239
|
7 |
|
$this->promptMatcher->getMatches() |
|
240
|
|
|
); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @return TelnetClientInterface |
|
245
|
|
|
*/ |
|
246
|
1 |
|
public static function factory() |
|
247
|
|
|
{ |
|
248
|
1 |
|
return new static( |
|
249
|
1 |
|
new SocketFactory(), |
|
250
|
1 |
|
new PromptMatcher(), |
|
251
|
1 |
|
new InterpretAsCommand() |
|
252
|
|
|
); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
8 |
|
public function __destruct() |
|
256
|
|
|
{ |
|
257
|
8 |
|
if (!$this->socket) { |
|
258
|
1 |
|
return; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
7 |
|
$this->socket->close(); |
|
262
|
7 |
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|