66[ ![ 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 )
77[ ![ 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/ )
88
9- # RouterOS PHP7 API Client
9+ # RouterOS API Client
1010
1111 composer require evilfreelancer/routeros-api-php
1212
@@ -17,6 +17,11 @@ to work with PHP7 in accordance with the PSR standards.
1717You can use this library with pre-6.43 and post-6.43 versions of
1818RouterOS firmware, it will be detected automatically on connection stage.
1919
20+ ## Minimum requirements
21+
22+ * ` php ` >= 7.2
23+ * ` ext-sockets `
24+
2025## Laravel framework support
2126
2227RouterOS 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([
3237$client = new \RouterOS\Client($config);
3338```
3439
35- Call facade and pass array of parameters to ` getClient ` method:
40+ Call facade and pass array of parameters to ` client ` method:
3641
3742``` php
38- $client = \RouterOS::getClient ([
43+ $client = \RouterOS::client ([
3944 'host' => '192.168.1.3',
4045 'user' => 'admin',
4146 'pass' => 'admin',
4247 'port' => 8728,
4348]);
4449```
4550
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+ ]);
4760
48- Install the package via Composer:
61+ dump($config);
4962
50- composer require evilfreelancer/routeros-api-php
63+ $client = \RouterOS::client($config);
64+ ```
5165
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
5470(after composer require is done, of course), add into` providers ` block of your ` config/app.php ` :
5571
5672``` php
5773'providers' => [
5874 // ...
59- RouterOS\Laravel\ClientServiceProvider ::class,
75+ RouterOS\Laravel\ServiceProvider ::class,
6076],
6177```
6278
6379Optionally, publish the configuration file if you want to change any defaults:
6480
65- php artisan vendor:publish --provider="RouterOS\\Laravel\\ClientServiceProvider "
81+ php artisan vendor:publish --provider="RouterOS\\Laravel\\ServiceProvider "
6682
6783## How to use
6884
@@ -88,18 +104,53 @@ $query =
88104$response = $client->query($query)->read();
89105
90106var_dump($response);
107+ ```
108+
109+ Basic example for update/create/delete types of queries:
91110
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
93123$query =
94124 (new Query('/ip/hotspot/ip-binding/add'))
95125 ->equal('mac-address', '00:00:00:00:40:29')
96126 ->equal('type', 'bypassed')
97127 ->equal('comment', 'testcomment');
98128
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)
100130$response = $client->query($query)->read();
131+
132+ var_dump($response);
101133```
102134
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+
103154Examples with "where" conditions, "operations" and "tag":
104155
105156``` php
@@ -257,10 +308,10 @@ need to create a "Query" object whose first argument is the
257308required command, after this you can add the attributes of the
258309command to "Query" object.
259310
260- More about attributes and "words" from which this attributes
311+ More about attributes and "words" from which these attributes
261312should be created [ here] ( https://wiki.mikrotik.com/wiki/Manual:API#Command_word ) .
262313
263- More about "expressions", "where" and other filters/modificators
314+ More about "expressions", "where", "equal" and other filters/modifications
264315of your query you can find [ here] ( https://wiki.mikrotik.com/wiki/Manual:API#Queries ) .
265316
266317Simple usage examples of Query class:
@@ -271,6 +322,13 @@ use \RouterOS\Query;
271322// Get all installed packages (it may be enabled or disabled)
272323$query = new Query('/system/package/getall');
273324
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+
274332// Set where interface is disabled and ID is ether1 (with tag 4)
275333$query =
276334 (new Query('/interface/set'))
@@ -285,7 +343,7 @@ $query =
285343 ->where('type', 'vlan')
286344 ->operations('|');
287345
288- /// Get all routes that have non-empty comment
346+ // Get all routes that have non-empty comment
289347$query =
290348 (new Query('/ip/route/print'))
291349 ->where('comment', '>', null);
@@ -357,8 +415,8 @@ $query->operations('|');
357415
358416// Enable interface (tag is 4)
359417$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');
362420$query->tag(4);
363421
364422// Or
@@ -396,8 +454,8 @@ $response = $client->query($query)->read();
396454
397455## Read response as Iterator
398456
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
401459in response from RouterOS API.
402460
403461But some routers may have (for example) 30000+ records in
@@ -468,6 +526,75 @@ $client->query($query1)->query($query2)->query($query3);
468526$client->q($query1)->q($query2)->q($query3);
469527```
470528
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+
471598## Testing
472599
473600You can use my [ other project] ( https://github.com/EvilFreelancer/docker-routeros )
0 commit comments