forked from cBackup/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelnet.php
223 lines (172 loc) · 4.62 KB
/
Telnet.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
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
<?php
namespace app\components;
/**
* TelnetException class
* Extension to throw specific exceptions
*/
class TelnetException extends \Exception
{
}
/**
* Simplified telnet class
*/
class Telnet
{
/**
* Timeout in seconds
* @var int
*/
private $timeout = 5;
/**
* Socket resource handler
* @var resource
*/
private $socket;
/**
* Property for storing raw data read from context
* @var string
*/
private $data;
/**
* Failed login string patterns
* @var array
*/
private $auth_fail = [
'/fail/i',
'/error/i',
];
/**
* @var string
*/
private $prompt = '/#/';
/**
* @return void
*/
public function __destruct()
{
if ( is_resource($this->socket) ) {
$this->send("logout\n");
fclose($this->socket);
}
}
/**
* Timeout setter
* Sets value for stream_set_timeout() second argument (only seconds)
*
* @param int $timeout
* @return $this
*/
public function setTimeout(int $timeout)
{
$this->timeout = intval($timeout);
if( $this->timeout < 1 ) {
$this->timeout = 1;
trigger_error('Timeout can not be less than 1 second', E_USER_NOTICE);
}
if( is_resource($this->socket) ) {
stream_set_timeout($this->socket, $this->timeout, 0);
}
return $this;
}
/**
* @param string $prompt
* @return $this
*/
public function setPrompt(string $prompt)
{
$this->prompt = $prompt;
return $this;
}
/**
* Return data which was read from socket
*
* @see waitfor()
* @return string
*/
public function getData()
{
return $this->data;
}
/**
* Socket initialization
*
* @param string $ip ip address
* @param int $port remote port
* @throws TelnetException
* @return $this
*/
public function connect($ip, $port=23)
{
if ( !is_resource($this->socket) ) {
$this->socket = stream_socket_client("$ip:$port", $errno, $errstr, $this->timeout);
}
if( is_resource($this->socket) ) {
stream_set_timeout($this->socket, $this->timeout, 0);
return $this;
}
throw new TelnetException('Unable to open connection');
}
/**
* Waiting for the line
*
* @param string $pattern regular expression to be waited for
* @param bool $silent if false - an error will be triggered in case of timeout
* if true - timeout error will be suppressed
* @return $this
*/
public function waitfor(string $pattern, bool $silent = false)
{
$start = microtime( true );
$this->data = '';
// Loop until timeout is hit or pattern is found
do {
$this->data.= fread( $this->socket, 1 );
$match = preg_match($pattern, $this->data);
if( (microtime(true) - $start) > $this->timeout ) {
if (!$silent) {
trigger_error("Reading socket timed out in $this->timeout seconds", E_USER_WARNING);
}
break;
}
} while ( $match == 0 );
return $this;
}
/**
* Send data
*
* @param string $data the data to be sent to the socket
* @param bool $newLine should 'enter' be added to the command
* @return bool
*/
public function send(string $data, bool $newLine = true): bool
{
$data = stream_socket_sendto($this->socket, $data);
if( $newLine ) {
$data = stream_socket_sendto($this->socket, "\n") + $data;
}
return boolval($data);
}
/**
* Authentication wrapper
*
* @param string $login username
* @param string $pass password
* @param array $prompt array with credentials
* two elements should be provided: [ login, password ]
* @throws TelnetException
* @return bool
*/
public function login(string $login, string $pass, array $prompt = ['/ame:/i', '/ord:/i']): bool
{
$this->waitfor($prompt[0], true)->send($login);
$this->waitfor($prompt[1], true)->send($pass);
// Read data
$raw = $this->waitfor($this->prompt, true)->getData();
foreach( $this->auth_fail as $pattern ) {
if( preg_match($pattern, $raw) ) {
throw new TelnetException('Authentication failed');
}
}
return true;
}
}