Download the PHP package proner/routeros-api-php without Composer
On this page you can find all versions of the php package proner/routeros-api-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package routeros-api-php
RouterOS API Client
composer require evilfreelancer/routeros-api-php
This library is partly based on this old project, but unlike it has many innovations to ease development. In addition, the project is designed to work with PHP7 in accordance with the PSR standards.
You can use this library with pre-6.43 and post-6.43 versions of RouterOS firmware, it will be detected automatically on connection stage.
Minimum requirements
php
>= 7.2ext-sockets
Laravel framework support
RouterOS API client is optimized for usage as normal Laravel package, all functional is available via \RouterOS
facade,
for access to client object you need instead:
Call facade and pass array of parameters to client
method:
You also may get array with all configs which was obtained from routeros-api.php
file:
Laravel installation
By default, the package will automatically register its service provider, but
if you are a happy owner of Laravel version less than 5.5, then in a project, which is using your package
(after composer require is done, of course), add intoproviders
block of your config/app.php
:
Optionally, publish the configuration file if you want to change any defaults:
php artisan vendor:publish --provider="RouterOS\\Laravel\\ServiceProvider"
How to use
Basic example, analogue via command line is /ip hotspot ip-binding print
:
Basic example for update/create/delete types of queries:
If you need export all settings from router:
Examples with "where" conditions, "operations" and "tag":
All available examples you can find here.
How to configure the client
You just need create object of Client class with required parameters in array format:
ℹ️ Advanced examples of Config and Client classes usage
List of available configuration parameters
Parameter | Type | Default | Description |
---|---|---|---|
host | string | (required) Address of Mikrotik RouterOS | |
user | string | (required) Username | |
pass | string | (required) Password | |
port | int | RouterOS API port number for access (if not set use 8728 or 8729 if SSL enabled) | |
ssl | bool | false | Enable ssl support (if port is not set this parameter must change default port to ssl port) |
legacy | bool | false | Support of legacy login scheme (true - pre 6.43, false - post 6.43) |
timeout | int | 10 | Max timeout for answer from RouterOS |
attempts | int | 10 | Count of attempts to establish TCP session |
delay | int | 1 | Delay between attempts in seconds |
How to enable support of legacy login schema (RouterOS pre-6.43)
From 0.8.1 this is not important, version of firmware will be detected automatically.
How to write queries
You can write absolutely any queries to your router, for this you need to create a "Query" object whose first argument is the required command, after this you can add the attributes of the command to "Query" object.
More about attributes and "words" from which these attributes should be created here.
More about "expressions", "where", "equal" and other filters/modifications of your query you can find here.
Simple usage examples of Query class:
ℹ️ Advanced examples of Query class usage
Read response as Iterator
By default, original solution of this client is not optimized for work with a large amount of results, only for small count of lines in response from RouterOS API.
But some routers may have (for example) 30000+ records in
their firewall list. Specifically for such tasks, a method
readAsIterator
has been added that converts the results
obtained from the router into a resource, with which it will
later be possible to work.
You could treat response as an array except using any array_* functions
Short methods
You can simplify your code and send then read from socket in one line:
Known issues
Unable to establish socket session, Operation timed out
This error means that the library cannot connect to your router, it may mean router turned off (then need turn on), or the API service not enabled.
Go to Mikrotik Router OS -> IP -> Services
and enable api
service.
Or via command line:
How to update/remove/create something via API?
Instead of ->where()
method of Query
class you need to
use ->equal()
method:
Undefined character (any non-English languages)
RouterOS does not support national languages, only English (and API of RouterOS too).
You can try to reproduce it via web, for example add the comment to any element of your system, then save and reload the page, you will see unreadable characters.
Testing
You can use my other project with RouterOS in Docker container for running unit testing on your computer, for this you just need to have Expect, Docker and Docker Compose.
Next clone the repo with RouterOS in Docker and exec
docker-compose up -d
, then you need preconfigure virtual routers
via preconf.tcl
script from root of routeros-api-php:
And after this you can run tests:
Links
- Cloud Hosted Router - Virtual images of RouterOS for your hypervisor
- RouterOS Manual:API - In case if you are wondering what is insane