Skip to content

Commit 2f822d8

Browse files
Merge pull request #40 from EvilFreelancer/development
Update to version 1.3
2 parents cbac8b7 + 71f5bf7 commit 2f822d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1036
-643
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/vendor/
33
/composer.lock
44
/clover.xml
5-
/.phpunit.result.cache
5+
/.phpunit.result.cache

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ before_script:
2727
- ./preconf.tcl 12223 > /dev/null || true
2828
- ./preconf.tcl 22223 > /dev/null || true
2929
- composer self-update
30-
- composer install --prefer-source --no-interaction --dev
30+
- composer install --no-interaction --dev
3131

3232
script:
3333
- vendor/bin/phpunit --coverage-clover=coverage.clover

README.md

Lines changed: 146 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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.
1717
You can use this library with pre-6.43 and post-6.43 versions of
1818
RouterOS 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

2227
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([
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

6379
Optionally, 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

90106
var_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+
103154
Examples with "where" conditions, "operations" and "tag":
104155

105156
```php
@@ -257,10 +308,10 @@ need to create a "Query" object whose first argument is the
257308
required command, after this you can add the attributes of the
258309
command to "Query" object.
259310

260-
More about attributes and "words" from which this attributes
311+
More about attributes and "words" from which these attributes
261312
should 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
264315
of your query you can find [here](https://wiki.mikrotik.com/wiki/Manual:API#Queries).
265316

266317
Simple 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
401459
in response from RouterOS API.
402460

403461
But 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

473600
You can use my [other project](https://github.com/EvilFreelancer/docker-routeros)

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,25 @@
3333
"extra": {
3434
"laravel": {
3535
"providers": [
36-
"RouterOS\\Laravel\\ClientServiceProvider"
36+
"RouterOS\\Laravel\\ServiceProvider"
3737
],
3838
"aliases": {
39-
"RouterOS": "RouterOS\\Laravel\\ClientFacade"
39+
"RouterOS": "RouterOS\\Laravel\\Facade"
4040
}
4141
}
4242
},
4343
"require": {
4444
"php": "^7.2",
45-
"ext-sockets": "*"
45+
"ext-sockets": "*",
46+
"divineomega/php-ssh-connection": "^2.2"
4647
},
4748
"require-dev": {
48-
"phpunit/phpunit": "^7.0",
49-
"orchestra/testbench": "^3.0",
49+
"limedeck/phpunit-detailed-printer": "^5.0",
50+
"orchestra/testbench": "^4.0|^5.0",
51+
"phpunit/phpunit": "^8.0",
5052
"roave/security-advisories": "dev-master",
51-
"squizlabs/php_codesniffer": "^3.5"
53+
"squizlabs/php_codesniffer": "^3.5",
54+
"larapack/dd": "^1.1"
5255
},
5356
"scripts": {
5457
"test": "phpunit --coverage-clover clover.xml",

configs/routeros-api.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
<?php
22

33
return [
4-
// 'host' => null,
5-
// 'user' => null,
6-
// 'pass' => null,
7-
// 'port' => null,
8-
'ssl' => false,
9-
'legacy' => false,
10-
'timeout' => 10,
11-
'attempts' => 10,
12-
'delay' => 1,
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Connection details
8+
|--------------------------------------------------------------------------
9+
|
10+
| Here you may specify different information about your router, like
11+
| hostname (or ip-address), username, password, port and ssl mode.
12+
|
13+
| SSH port should be set if you want to use "/export" command.
14+
|
15+
*/
16+
17+
'host' => '192.168.88.1', // Address of Mikrotik RouterOS
18+
'user' => 'admin', // Username
19+
'pass' => null, // Password
20+
'port' => 8728, // RouterOS API port number for access (if not set use default or default with SSL if SSL enabled)
21+
'ssl' => false, // Enable ssl support (if port is not set this parameter must change default port to ssl port)
22+
'ssh_port' => 22, // Number of SSH port
23+
24+
/*
25+
|--------------------------------------------------------------------------
26+
| Optional connection settings of client
27+
|--------------------------------------------------------------------------
28+
|
29+
| Settings bellow need to advanced tune of your connection, for example
30+
| you may enable legacy mode by default, or change timeout of connection.
31+
|
32+
*/
33+
34+
'legacy' => false, // Support of legacy login scheme (true - pre 6.43, false - post 6.43)
35+
'timeout' => 10, // Max timeout for answer from RouterOS
36+
'attempts' => 10, // Count of attempts to establish TCP session
37+
'delay' => 1, // Delay between attempts in seconds
38+
1339
];

examples/bridge_hosts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
$query = new Query('/interface/bridge/host/print');
2323

2424
// Send query to RouterOS
25-
$response = $client->write($query)->read();
25+
$response = $client->query($query)->read();
2626
print_r($response);

examples/different_queries.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
]);
1515

1616
for ($i = 0; $i < 10; $i++) {
17-
$response = $client->wr('/ip/address/print');
17+
$response = $client->qr('/ip/address/print');
1818
print_r($response);
1919

20-
$response = $client->wr('/ip/arp/print');
20+
$response = $client->qr('/ip/arp/print');
2121
print_r($response);
2222

23-
$response = $client->wr('/interface/print');
23+
$response = $client->qr('/interface/print');
2424
print_r($response);
2525
}

0 commit comments

Comments
 (0)