-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRouterOS.php
530 lines (447 loc) · 15.4 KB
/
RouterOS.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
/**
*
* Copyright (c) 2012, Drew Phillips
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category RouterOS
* @package API
* @copyright Copyright (c) 2012 Drew Phillips
* @license BSD
* @author Drew Phillips <drew@drew-phillips.com>
* @version 1.0
*
*/
class Lib_RouterOS
{
const VERSION = '1.0';
protected $_conn;
protected $_authenticated;
protected $_debug;
protected $_connected;
protected $_router;
protected $_routerPort;
protected $_username;
protected $_password;
/*%*********************************************************************%*/
// Constructor and public methods
public function __construct(array $options = array())
{
// set defaults
$this->_router = '192.168.88.1';
$this->_routerPort = 8728;
$this->_username = 'admin';
$this->_password = '';
$this->_authenticated = false;
$this->_debug = false;
$this->_connected = false;
// attempt to set any options provided
if (is_array($options) && sizeof($options) > 0) {
foreach($options as $opt => $value) {
$method = 'set' . ucfirst($opt);
if (method_exists($this, $method)) {
$this->$method($value);
} else {
trigger_error('Attempting to set unknown option "' . $opt . '" in constructor', E_USER_NOTICE);
}
}
}
}
function __destruct()
{
if ($this->_connected == true) {
$this->disconnect();
} else if ($this->_conn) {
@fclose($this->_conn);
}
}
/**
* Connect and log in to a MikroTik router
*
* @param string $router The ip address or hostname (and optional :port) of the router to log into
* @param string $username The user to login as (default 'admin')
* @param string $password The user's password (default null)
* @param bool $connectOnDemand default: false - connect and authenticate immediately; true - connect and auth on first command
* @throws Exception If login fails or on !trap/!failure
*/
public function connect($router, $username = 'admin', $password = null, $connectOnDemand = false)
{
if ($this->_connected) {
throw new Exception('Already connected, cannot call connect()');
}
if (strpos($router, ':') !== false) {
list($router, $port) = explode(':', $router, 2);
$this->_routerPort = $port;
}
$this->_router = $router;
$this->_username = $username;
$this->_password = $password;
$errno = ''; $errstr = '';
if (!$connectOnDemand) {
$this->_debug("Connecting to $router...");
$this->_conn = @fsockopen($router, $this->_routerPort, $errno, $errstr, 10);
if ($this->_conn) {
$this->_debug("Connected to router");
$this->_connected = true;
if ($username != '' && $password != null) {
try {
$this->login($username, $password);
} catch(Exception $ex) {
throw $ex;
}
}
} else {
throw new Exception("Failed to connect to router. Code $errno: $errstr");
}
}
return true;
}
/**
* Close connection to the router
*
* @return boolean true if successfully disconnected, false if failed
*/
public function disconnect()
{
if (!$this->_connected) return true;
$this->send('/quit');
try {
$resp = $this->read();
} catch (Exception $ex) {
$this->_connected = false;
$this->_authenticated = false;
return true;
}
return false;
}
/**
* Attempt to authenticate with the RouterOS API. Router IP/host should be set already otherwise defaults to 192.168.88.1
*
* @param string $username Username to log in as
* @param string $password Password for username
* @throws Exception If login fails, exception is thrown
*/
public function login($username, $password)
{
$this->_username = $username;
$this->_password = $password;
if ($this->_authenticated) return true;
if (!$this->_connected) try { $this->_doConnect(); } catch (Exception $ex) { throw $ex; }
$this->_debug("Starting login sequence");
$this->send("/login");
$resp = $this->read();
$md5 = md5(chr(0) . $password . pack('H*', $resp['!done']['ret']));
$this->send('/login', array('name' => $username,
'response' => '00' . $md5));
try {
$resp = $this->read();
if (!isset($resp['!done'])) {
throw new Exception('Login failed for unknown reason, !done not found');
}
} catch (Exception $ex) {
throw $ex;
}
$this->_authenticated = true;
return true;
}
/**
* Gets the identity (given name) of the connected router
* @throws Exception Throws exception on !trap or !fatal, or if login fails
* @return <string>|boolean Returns false if identity not found, or the identity
*/
public function getRouterIdentity()
{
try {
$this->send('/system/identity/print');
$resp = $this->read();
foreach($resp as $k => $r) {
if ($k === '!done') continue;
if (isset($r['name']) && trim($r['name']) != '') {
return $r['name'];
}
}
return false;
} catch (Exception $ex) {
throw $ex;
}
}
/**
* Send one or more commands to the router
*
* @param string|array $commands A command or array of commands to send
* @param array $args An optional array of name => value parameters to send encoded as =name=value
*/
public function send($commands, $args = array())
{
if (!$this->_connected) try { $this->_doConnect(); } catch (Exception $ex) { throw $ex; }
if (!is_array($commands)) $commands = array($commands);
if (is_array($args) && sizeof($args) > 0) {
foreach($args as $attr => $value) {
$commands[] = "={$attr}={$value}";
}
}
foreach($commands as $command) {
$len = $this->encodeLength(strlen($command));
$this->_debug(">>> $len $command");
$writ = fputs($this->_conn, $len . $command);
}
if ($writ !== false) {
$writ = fputs($this->_conn, $this->encodeLength(0));
}
if ($writ === false) {
$this->_connected = false;
throw new Exception('Failed to send command - connection terminated');
}
}
/**
* Read data from the API (if available). This function will block if called and no data is available to be read
*
* @throws Exception If !trap or !fatal error result, or if failed to read length of command response
* @return array Returns array of data parsed from the response
*/
public function read()
{
if (!$this->_connected) try { $this->_doConnect(); } catch (Exception $ex) { throw $ex; }
$ret = array();
$trap = null;
while(1) {
$len = $this->readLength();
if ($len === false) {
$this->_connected = false;
throw new Exception('Failed to read length - connection terminated');
}
$word = $this->readWord($len);
if (strlen($word) == 0) continue;
$reply = $word;
$attrs = array();
while( ($len = $this->readLength()) > 0 ) {
$w = $this->readWord($len);
if ($reply == '!re') {
$reply = '';
}
$p = strpos($w, '=', 1);
if ($p !== false) {
$attrs[substr($w, 1, $p - 1)] = substr($w, $p + 1);
} else {
$attrs[$w] = '';
}
}
if ($reply == '') {
$ret[] = $attrs;
} else {
$ret[$reply] = $attrs;
}
if ($reply == '!trap') {
if ($trap != null) {
$trap .= "Trap: {$attrs['message']}";
} else {
$trap = "Trap: {$attrs['message']}";
}
} else if ($reply == '!fatal') {
if (sizeof($attrs) > 0) {
$keys = array_keys($attrs);
$message = array_shift($keys);
} else {
$message = 'Unknown Error';
}
fclose($this->_conn);
$this->_conn = null;
$this->_connected = false;
throw new Exception ("Fatal: $message");
}
if ($reply == '!done') {
if ($trap != null) throw new Exception($trap);
return $ret;
}
}
}
/**
* Read a word of given $length from the API socket
* @param int $length The length of the data to read
* @throws Exception If connection lost during read
*/
public function readWord($length)
{
$w = '';
if ($length > 0) {
$read = 0;
while($read < $length) {
$s = fread($this->_conn, $length - $read);
if ($s === false) {
throw new Exception('Connection to router was lost');
}
$w .= $s;
$read += strlen($w);
}
$this->_debug("<<< $w");
}
return $w;
}
/**
* Read an encoded length from the API socket
* @return bool|int false if no data to be read, or the length of the next data chunk
*/
public function readLength()
{
$len = fread($this->_conn, 1);
if ($len === false) return false;
if ( ($len & 0x80) == 0) {
$len = ord($len);
} else if ( ($len & 0xC0) == 0x80) {
$len &= ~0xC0;
$len <<= 8;
$len += ord(fread($this->_conn, 1));
} else if ( ($len & 0xE0) == 0xC0) {
$len &= ~0xE0;
$len <<= 8;
$len += ord(fread($this->_conn, 1));
$len <<= 8;
$len += ord(fread($this->_conn, 1));
} else if ( ($len & 0xF0) == 0xE0) {
$len &= ~0xF0;
$len <<= 8;
$len += ord(fread($this->_conn, 1));
$len <<= 8;
$len += ord(fread($this->_conn, 1));
$len <<= 8;
$len += ord(fread($this->_conn, 1));
} else if ( ($len & 0xF8) == 0xF0) {
$len = ord(fread($this->_conn, 1));
$len <<= 8;
$len += ord(fread($this->_conn, 1));
$len <<= 8;
$len += ord(fread($this->_conn, 1));
$len <<= 8;
$len += ord(fread($this->_conn, 1));
}
return $len;
}
/**
* Get an encoded length for sending to the API
* @param int $length The length of the command to encode
* @return string Encoded length
*/
public function encodeLength($length)
{
if ($length <= 0x7F) {
return pack('C', $length);
} else if ($length <= 0x3FFF) {
return pack('n', ($length | 0x8000));
} else if ($length <= 0x1FFFFF) {
$length |= 0xC00000;
return pack('C3', ($length >> 16) & 0xFF, ($length >> 8) & 0xFF, ($length & 0xFF));
} else if ($length <= 0xFFFFFFF) {
return pack('N', ($length | 0xE0000000));
} else {
return pack('CN', 0xF0, $length);
}
}
/*%*********************************************************************%*/
// Getters and Setters
public function getConnected()
{
return $this->_connected;
}
public function getAuthenticated()
{
return $this->_authenticated;
}
public function getRouter()
{
return $this->_router;
}
public function setRouter($router)
{
$this->_router = $router;
return $this;
}
public function getRouterPort()
{
return $this->_routerPort;
}
public function setRouterPort($routerPort)
{
$this->_routerPort = $routerPort;
return $this;
}
public function getUsername()
{
return $this->_username;
}
public function setUsername($username)
{
$this->_username = $username;
return $this;
}
public function getPassword()
{
return $this->_password;
}
public function setPassword($password)
{
$this->_password = $password;
return $this;
}
/**
* Enable debug output
* @param bool $debug true to enable debug output, false to disable (default)
*/
public function setDebug($debug)
{
$this->_debug = (bool)$debug;
return $this;
}
/**
* Get the value of the debug setting
* @return boolean true if debugging enabled, false if disabled
*/
public function getDebug()
{
return $this->_debug;
}
/*%*********************************************************************%*/
// Protected methods
/**
* Helper function used by many other functions to connect to the router and support connectOnDemand option
* @throws Exception
*/
protected function _doConnect()
{
try {
$this->connect($this->_router, $this->_username, $this->_password);
} catch (Exception $ex) {
throw $ex;
}
}
/**
* Output a debug message to the console
* @param string $message Message to display
*/
protected function _debug($message)
{
if ($this->_debug) {
echo sprintf("%s: %s\n", date('r'), $message);
}
}
}