You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

3.4 KiB

Latest Stable Version Total Downloads License Scrutinizer CQ

RouterOS PHP7 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.

If you want to help the project, I will be glad to any help, my twitter @EvilFreelancer.

Known issues

This library is not ready for production usage, because yet is not implemented new login scheme for post 6.43 firmwares (but it works with pre 6.43).

In addition, need to implement a full test of everything through phpUnit, as well as write more detailed documentation and add more examples.

This issues will be fixed in future releases.

Small example

Get all IP addresses, analog via command line is /ip address print

<?php
require_once __DIR__ . '/vendor/autoload.php';

error_reporting(E_ALL);

use \RouterOS\Config;
use \RouterOS\Client;
use \RouterOS\Query;

/**
 * Set the params
 */
$config = new Config();
$config->host = '192.168.1.104';
$config->user = 'admin';
$config->pass = 'admin';

/**
 * Initiate client with parameters
 */
$client = new Client($config);

/**
 * Build query
 */
$query = new Query('/ip/address/print');

/**
 * Send query to socket server
 */
$request = $client->write($query);
var_dump($request);

/**
 * Read answer from server
 */
$response = $client->read();
var_dump($response);

You can simplify your code and write then read from socket in one line:

$response = $client->write($query)->read();
var_dump($response);

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 this attributes should be created here.

use \RouterOS\Query;

// One line query: Get all packages
$query = new Query('/system/package/getall');

// Multiline query: Enable interface and add tag
$query = new Query('/interface/set');
$query
    ->add('=disabled=no')
    ->add('=.id=ether1')
    ->add('.tag=4');

// Multiline query: Get all ethernet and VLAN interfaces
$query = new Query('/interface/print');
$query
    ->add('?type=ether')
    ->add('?type=vlan')
    ->add('?#|');

// Multiline query: Get all routes that have non-empty comment
$query = new Query('/ip/route/print');
$query
    ->add('?>comment=');