6
6
[ ![ Code Coverage] ( https://scrutinizer-ci.com/g/EvilFreelancer/routeros-api-php/badges/coverage.png?b=master )] ( https://scrutinizer-ci.com/g/EvilFreelancer/routeros-api-php/?branch=master )
7
7
[ ![ Scrutinizer CQ] ( https://scrutinizer-ci.com/g/evilfreelancer/routeros-api-php/badges/quality-score.png?b=master )] ( https://scrutinizer-ci.com/g/evilfreelancer/routeros-api-php/ )
8
8
9
- # RouterOS PHP7 API Client
9
+ # RouterOS API Client
10
10
11
11
composer require evilfreelancer/routeros-api-php
12
12
@@ -17,6 +17,11 @@ to work with PHP7 in accordance with the PSR standards.
17
17
You can use this library with pre-6.43 and post-6.43 versions of
18
18
RouterOS firmware, it will be detected automatically on connection stage.
19
19
20
+ ## Minimum requirements
21
+
22
+ * ` php ` >= 7.2
23
+ * ` ext-sockets `
24
+
20
25
## Laravel framework support
21
26
22
27
RouterOS API client is optimized for usage as normal Laravel package, all functional is available via ` \RouterOS ` facade,
@@ -32,37 +37,48 @@ $config = new \RouterOS\Config([
32
37
$client = new \RouterOS\Client($config);
33
38
```
34
39
35
- Call facade and pass array of parameters to ` getClient ` method:
40
+ Call facade and pass array of parameters to ` client ` method:
36
41
37
42
``` php
38
- $client = \RouterOS::getClient ([
43
+ $client = \RouterOS::client ([
39
44
'host' => '192.168.1.3',
40
45
'user' => 'admin',
41
46
'pass' => 'admin',
42
47
'port' => 8728,
43
48
]);
44
49
```
45
50
46
- ### Laravel installation
51
+ You also may get array with all configs which was obtained from ` routeros-api.php ` file:
52
+
53
+ ``` php
54
+ $config = \RouterOS::config([
55
+ 'host' => '192.168.1.3',
56
+ 'user' => 'admin',
57
+ 'pass' => 'admin',
58
+ 'port' => 8728,
59
+ ]);
47
60
48
- Install the package via Composer:
61
+ dump($config);
49
62
50
- composer require evilfreelancer/routeros-api-php
63
+ $client = \RouterOS::client($config);
64
+ ```
51
65
52
- By default the package will automatically register its service provider, but
53
- if you are a happy owner of Laravel version less than 5.3, then in a project, which is using your package
66
+ ### Laravel installation
67
+
68
+ By default, the package will automatically register its service provider, but
69
+ if you are a happy owner of Laravel version less than 5.5, then in a project, which is using your package
54
70
(after composer require is done, of course), add into` providers ` block of your ` config/app.php ` :
55
71
56
72
``` php
57
73
'providers' => [
58
74
// ...
59
- RouterOS\Laravel\ClientServiceProvider ::class,
75
+ RouterOS\Laravel\ServiceProvider ::class,
60
76
],
61
77
```
62
78
63
79
Optionally, publish the configuration file if you want to change any defaults:
64
80
65
- php artisan vendor:publish --provider="RouterOS\\Laravel\\ClientServiceProvider "
81
+ php artisan vendor:publish --provider="RouterOS\\Laravel\\ServiceProvider "
66
82
67
83
## How to use
68
84
@@ -88,18 +104,53 @@ $query =
88
104
$response = $client->query($query)->read();
89
105
90
106
var_dump($response);
107
+ ```
108
+
109
+ Basic example for update/create/delete types of queries:
91
110
92
- // Send "equal" query
111
+ ``` php
112
+ use \RouterOS\Client;
113
+ use \RouterOS\Query;
114
+
115
+ // Initiate client with config object
116
+ $client = new Client([
117
+ 'host' => '192.168.1.3',
118
+ 'user' => 'admin',
119
+ 'pass' => 'admin'
120
+ ]);
121
+
122
+ // Send "equal" query with details about IP address which should be created
93
123
$query =
94
124
(new Query('/ip/hotspot/ip-binding/add'))
95
125
->equal('mac-address', '00:00:00:00:40:29')
96
126
->equal('type', 'bypassed')
97
127
->equal('comment', 'testcomment');
98
128
99
- // Send query and read response from RouterOS (ordinary answer to update/create/delete queries has empty body)
129
+ // Send query and read response from RouterOS (ordinary answer from update/create/delete queries has empty body)
100
130
$response = $client->query($query)->read();
131
+
132
+ var_dump($response);
101
133
```
102
134
135
+ If you need export all settings from router:
136
+
137
+ ``` php
138
+ use \RouterOS\Client;
139
+
140
+ // Initiate client with config object
141
+ $client = new Client([
142
+ 'host' => '192.168.1.3',
143
+ 'user' => 'admin',
144
+ 'pass' => 'admin',
145
+ 'ssh_port' => 22222,
146
+ ]);
147
+
148
+ // Execute export command via ssh, because API /export method has a bug
149
+ $response = $client->export();
150
+
151
+ print_r($response);
152
+ ```
153
+
103
154
Examples with "where" conditions, "operations" and "tag":
104
155
105
156
``` php
@@ -257,10 +308,10 @@ need to create a "Query" object whose first argument is the
257
308
required command, after this you can add the attributes of the
258
309
command to "Query" object.
259
310
260
- More about attributes and "words" from which this attributes
311
+ More about attributes and "words" from which these attributes
261
312
should be created [ here] ( https://wiki.mikrotik.com/wiki/Manual:API#Command_word ) .
262
313
263
- More about "expressions", "where" and other filters/modificators
314
+ More about "expressions", "where", "equal" and other filters/modifications
264
315
of your query you can find [ here] ( https://wiki.mikrotik.com/wiki/Manual:API#Queries ) .
265
316
266
317
Simple usage examples of Query class:
@@ -271,6 +322,13 @@ use \RouterOS\Query;
271
322
// Get all installed packages (it may be enabled or disabled)
272
323
$query = new Query('/system/package/getall');
273
324
325
+ // Send "equal" query with details about IP address which should be created
326
+ $query =
327
+ (new Query('/ip/hotspot/ip-binding/add'))
328
+ ->equal('mac-address', '00:00:00:00:40:29')
329
+ ->equal('type', 'bypassed')
330
+ ->equal('comment', 'testcomment');
331
+
274
332
// Set where interface is disabled and ID is ether1 (with tag 4)
275
333
$query =
276
334
(new Query('/interface/set'))
@@ -285,7 +343,7 @@ $query =
285
343
->where('type', 'vlan')
286
344
->operations('|');
287
345
288
- /// Get all routes that have non-empty comment
346
+ // Get all routes that have non-empty comment
289
347
$query =
290
348
(new Query('/ip/route/print'))
291
349
->where('comment', '>', null);
@@ -357,8 +415,8 @@ $query->operations('|');
357
415
358
416
// Enable interface (tag is 4)
359
417
$query = new Query('/interface/set');
360
- $query->where ('disabled', 'no');
361
- $query->where ('.id', 'ether1');
418
+ $query->equal ('disabled', 'no');
419
+ $query->equal ('.id', 'ether1');
362
420
$query->tag(4);
363
421
364
422
// Or
@@ -396,8 +454,8 @@ $response = $client->query($query)->read();
396
454
397
455
## Read response as Iterator
398
456
399
- By default original solution of this client is not optimized for
400
- work with large amount of results, only for small count of lines
457
+ By default, original solution of this client is not optimized for
458
+ work with a large amount of results, only for small count of lines
401
459
in response from RouterOS API.
402
460
403
461
But some routers may have (for example) 30000+ records in
@@ -468,6 +526,75 @@ $client->query($query1)->query($query2)->query($query3);
468
526
$client->q($query1)->q($query2)->q($query3);
469
527
```
470
528
529
+ ## Known issues
530
+
531
+ ### Unable to establish socket session, Operation timed out
532
+
533
+ This error means that the library cannot connect to your router,
534
+ it may mean router turned off (then need turn on), or the API service not enabled.
535
+
536
+ Go to ` Mikrotik Router OS -> IP -> Services ` and enable ` api ` service.
537
+
538
+ Or via command line:
539
+
540
+ ``` shell script
541
+ /ip service enable api
542
+ ```
543
+
544
+ ### How to update/remove/create something via API?
545
+
546
+ Instead of ` ->where() ` method of ` Query ` class you need to
547
+ use ` ->equal() ` method:
548
+
549
+ ``` php
550
+ // Create query which should remove security profile
551
+ $query = new \RouterOS\Query('/interface/wireless/security-profiles/remove');
552
+
553
+ // It will generate queries, which stared from "?" symbol:
554
+ $query->where('.id', '*1');
555
+
556
+ /*
557
+ // Sample with ->where() method
558
+ RouterOS\Query Object
559
+ (
560
+ [_attributes:RouterOS\Query:private] => Array
561
+ (
562
+ [0] => ?.id=*1
563
+ )
564
+
565
+ [_operations:RouterOS\Query:private] =>
566
+ [_tag:RouterOS\Query:private] =>
567
+ [_endpoint:RouterOS\Query:private] => /interface/wireless/security-profiles/remove
568
+ )
569
+ */
570
+
571
+ // So, as you can see, instead of `->where()` need to use `->equal()`
572
+ // It will generate queries, which stared from "=" symbol:
573
+ $query->equal('.id', '*1');
574
+
575
+ /*
576
+ // Sample with ->equal() method
577
+ RouterOS\Query Object
578
+ (
579
+ [_attributes:RouterOS\Query:private] => Array
580
+ (
581
+ [0] => =.id=*1
582
+ )
583
+
584
+ [_operations:RouterOS\Query:private] =>
585
+ [_tag:RouterOS\Query:private] =>
586
+ [_endpoint:RouterOS\Query:private] => /interface/wireless/security-profiles/remove
587
+ )
588
+ */
589
+ ```
590
+
591
+ ### Undefined character (any non-English languages)
592
+
593
+ RouterOS does not support national languages, only English (and API of RouterOS too).
594
+
595
+ You can try to reproduce it via web, for example add the comment to any
596
+ element of your system, then save and reload the page, you will see unreadable characters.
597
+
471
598
## Testing
472
599
473
600
You can use my [ other project] ( https://github.com/EvilFreelancer/docker-routeros )
0 commit comments