-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.php
More file actions
215 lines (197 loc) · 7.27 KB
/
Client.php
File metadata and controls
215 lines (197 loc) · 7.27 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
<?php
namespace Skrip42\Telnet;
use Psr\Log\LoggerInterface;
use Skrip42\Telnet\Components\CommandSequence;
use Skrip42\Telnet\Components\Command;
use Skrip42\Telnet\Components\Option;
use Skrip42\Telnet\Components\Printer;
use Skrip42\Telnet\Exceptions\TelnetException;
class Client
{
const BYTE_READ = 4096; //4kb
const READ_TIMEOUT = 10000; //10ms
private $socket;
private $buffer = '';
/** @var int $timelimit */
private $timelimit;
/** @var LoggerInterface $logger */
private $logger;
/** @var string $promtPattern */
private $promtPattern;
public function __construct(
string $ip,
int $port = 23,
int $timelimit = 100, //100ms
LoggerInterface $logger = null
) {
$this->logger = $logger;
$this->timelimit = $timelimit * 1000;
$this->socket = fsockopen($ip, $port);
// stream_set_timeout($this->socket, self::READ_TIMEOUT);
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_block($this->socket);
socket_connect($this->socket, $ip, $port);
}
public function __desctruct()
{
$sequense = new CommandSequence();
$sequense->addText('exit');
$this->sendSequence($sequense);
// fclose($this->socket);
socket_close($this->socket);
}
public function setPromtPattern(string $promtPattern) : self
{
$this->promtPattern = $promtPattern;
return $this;
}
public function setLogger(LoggerInterface $logger) : self
{
$this->setLogger($logger);
return $this;
}
public function login(string $login, string $password, string $promtPattern = null) : CommandSequence
{
//set telnet connetion options
$sequense = new CommandSequence();
$sequense
->addCommand(Command::DO, Option::SUPPRESS_GO_AHEAD)
->addCommand(Command::WILL, Option::TERMINAL_TYPE)
->addCommand(Command::WILL, Option::WINDOW_SIZE)
->addCommand(Command::WILL, Option::TERMINAL_SPEED)
->addCommand(Command::WILL, Option::REMOTE_FLOW_CONTROL)
->addCommand(Command::WILL, Option::TERMINAL_LINEMODE)
->addCommand(Command::WILL, Option::ENVIRONMENT)
->addCommand(Command::DO, Option::STATUS)
->addCommand(Command::WILL, Option::X_DISPLAY_LOCATION);
$this->sendSequence($sequense);
//get telnet response
$sequense = new CommandSequence();
$sequense->addCommand(Command::DONT, Option::X_DISPLAY_LOCATION);
$this->awaitSequence($sequense);
//enable authorization
$sequense = new CommandSequence();
$sequense
->addCommand(Command::DO, Option::SUPPRESS_GO_AHEAD)
->addCommand(Command::WILL, Option::ECHO);
$this->sendSequence($sequense);
//set login
$sequense = new CommandSequence();
$sequense->addText($login, Printer::CR);
$this->sendSequence($sequense);
$sequense = new CommandSequence();
$sequense->addText('Password:');
$this->awaitSequence($sequense);
//set password
$sequense = new CommandSequence();
$sequense->addText($password, Printer::CR);
$this->sendSequence($sequense);
return $this->awaitPrompt($promtPattern ?? $this->promtPattern);
}
public function sendMessage(string $message, string $promtPattern = null, int $timelimit = null) : string
{
if (empty($timelimit)) {
$timelimit = $this->timelimit;
} else {
$timelimit *= 1000;
}
if (empty($promtPattern)) {
$promtPattern = $this->promtPattern;
}
$sequense = new CommandSequence();
$sequense->addText($message, Printer::CR);
$this->sendSequence($sequense);
$result = $this->awaitPrompt($promtPattern, $timelimit);
return $result->getText();
}
public function sendSequence(CommandSequence $sequence)
{
if (!empty($this->logger)) {
$this->logger->info('send: ' . $sequence->dump());
}
socket_write($this->socket, $sequence->compile());
}
public function awaitSequence(CommandSequence $sequence, int $timelimit = null) : CommandSequence
{
if (empty($timelimit)) {
$timelimit = $this->timelimit;
} else {
$timelimit *= 1000;
}
$needle = $sequence->compile();
$ep = 0;
$counter = 0;
socket_set_nonblock($this->socket);
while (($ep = strpos($this->buffer, $needle, 0)) === false
&& $counter < ($timelimit / self::READ_TIMEOUT)
) {
usleep(self::READ_TIMEOUT);
$res = socket_read($this->socket, self::BYTE_READ);
if ($res !== false) {
$this->buffer .= $res;
}
$counter++;
}
socket_set_block($this->socket);
if ($counter >= $timelimit / self::READ_TIMEOUT) {
if (!empty($this->logger)) {
$this->logger->error(
'expected: ' . $sequence->dump()
. 'receive: ' . $this->buffer
);
}
throw new TelnetException('telnet swquence await timeout exceeded, response: ' . $this->buffer);
}
$ep += strlen($needle);
$raw = substr($this->buffer, 0, $ep);
$this->buffer = substr($this->buffer, $ep);
$sequence = new CommandSequence($raw);
if (!empty($this->logger)) {
$this->logger->info('receive: ' . $sequence->dump() . ' time: ' . $counter * self::READ_TIMEOUT / 1000 . 'ms');
}
return $sequence;
}
public function awaitPrompt(string $promtPattern = null, int $timelimit = null) : CommandSequence
{
if (empty($promtPattern)) {
$promtPattern = $this->promtPattern;
}
if (empty($timelimit)) {
$timelimit = $this->timelimit;
} else {
$timelimit *= 1000;
}
$counter = 0;
$match = [];
socket_set_nonblock($this->socket);
while (!preg_match($promtPattern, $this->buffer, $match, PREG_OFFSET_CAPTURE)
&& $counter < ($timelimit / self::READ_TIMEOUT)
) {
usleep(self::READ_TIMEOUT);
$res = socket_read($this->socket, self::BYTE_READ);
if (!empty($res !== false)) {
$this->buffer .= $res;
}
$counter++;
}
socket_set_block($this->socket);
if ($counter >= $timelimit / self::READ_TIMEOUT) {
if (!empty($this->logger)) {
$this->logger->error(
'expected: ' . $promtPattern
. 'receive: ' . $this->buffer
);
}
throw new TelnetException('telnet promt await timeout exceeded, response: ' . $this->buffer);
}
$ep = $match[0][1];
$ep += strlen($match[0][0]);
$raw = substr($this->buffer, 0, $ep);
$this->buffer = substr($this->buffer, $ep);
$sequence = new CommandSequence($raw);
if (!empty($this->logger)) {
$this->logger->info('receive: ' . $sequence->dump() . ' time: ' . $counter * self::READ_TIMEOUT / 1000 . 'ms');
}
return $sequence;
}
}