PHP code example of ocolin / routeros

1. Go to this page and download the library: Download ocolin/routeros library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ocolin / routeros example snippets


use Ocolin\RouterOS\Client;

$client = new Client([
    'host'     => '192.168.88.1',
    'username' => 'admin',
    'password' => 'secret'
]);

$interfaces = $client->query( '/interface/print' );

foreach( $interfaces as $interface ) {
    echo $interface['name'] . "\n";
}

$client = new Client([
    'host'     => '192.168.88.1',
    'username' => 'admin',
    'password' => 'secret'
]);

$config = new Config([
    'host' => '192.168.88.1',
]);

$client = new Client( $config );

// .env file
ROUTEROS_HOST=192.168.88.1
ROUTEROS_USERNAME=admin
ROUTEROS_PASSWORD=secret

// .env file
ROUTEROS_CORE_HOST=10.0.0.1
ROUTEROS_EDGE_HOST=10.0.0.2

$core = new Client( envPrefix: 'CORE' );
$edge = new Client( envPrefix: 'EDGE' );

$interfaces = $client->query( command: '/interface/print' );

foreach( $interfaces as $interface )
{
    echo $interface['name'] . PHP_EOL;
}


$interfaces = $client->query( command: [
    '/interface/print',
    '=disabled=no',
    '?type=ether'
]);
foreach( $interfaces as $interface )
{
    echo $interface['name'] . PHP_EOL;
}

use Ocolin\RouterOS\Command;

$interfaces = $client->query(
    command: new Command('/interface/print')
        ->attribute( key: 'disabled', value: false )
        ->query( 'type' )->equals( 'ether' )
);
foreach( $interfaces as $interface )
{
    echo $interface['name'] . PHP_EOL;
}

$command = new Command( '/interface/print' );

$command = new Command( '/interface/print' )
    ->attribute( key: 'disabled', value: true );

// Get interfaces of type equal to ether
$command = new Command( '/interface/print' )
    ->query( key: 'type', value: 'ether', operator: '=' );

// Get interfaces with any link-downs
$command = new Command( '/interface/print' )
    ->query( key: 'link-downs', value: 0, operator: '>' );

// Get interfaces of type equal to ether
$command = new Command( '/interface/print' )
    ->query( key: 'type' )->equals( 'ether' );

// Get interfaces with any no link-downs
$command = new Command( '/interface/print' )
    ->query( key: 'link-downs' )->lessThan( 1 );

// Get interfaces with link-downs
$command = new Command( '/interface/print' )
    ->query( key: 'link-downs' )->greaterThan( 0 );

// Get interfaces with link-downs property
$command = new Command( '/interface/print' )
    ->query( key: 'link-downs' )->exists();

// Get interfaces without link-downs property
$command = new Command( '/interface/print' )
    ->query( key: 'link-downs' )->notExists();

// Get interfaces of type equal to ether AND has link-downs
$command = new Command( '/interface/print' )
    ->query( key: 'type' )->equals( 'ether' )
    ->query( key: 'link-downs' )->greaterThan( 0 )
    ->and();

// Get interfaces of type equal to ether OR has link-downs
$command = new Command( '/interface/print' )
    ->query( key: 'type' )->equals( 'ether' )
    ->query( key: 'link-downs' )->greaterThan( 0 )
    ->or();

// Get interfaces of type not a vlan
$command = new Command( '/interface/print' )
    ->query( key: 'type' )->equals( 'vlan' )
    ->not();

$command = new Command( '/interface/print' )->proplist(['name', 'type']);

$command = new Command( '/interface/print' )->tag( 123 );

$command = new Command( '/interface/print' )->tag( 'abc' );

// Get interfaces without link-downs property
$command = new Command( '/interface/print' )
    ->where( key: 'link-downs' )->notExists();

$command = new Command( '/interface/print' )
    ->equal( key: 'disabled', value: true );

$command = new Command( '/interface/print' )
    ->query( 'type' )->equals( 'ether' )
    ->query( 'type' )->equals( 'vlan' )
    ->operations( '|' );

foreach( $client->stream( '/interface/listen' ) as $update ) {
    echo $update['name'] . ' changed state' . PHP_EOL;
    
    // Stop listening after first update
    if( $someCondition ) {
        break;
    }
}

$client = new Client([
    'host'      => '192.168.88.1',
    'username'  => 'admin',
    'password'  => 'secret',
    'ssl'       => true,
    'sslVerify' => false  // set to true if using a trusted certificate
]);

use Ocolin\RouterOS\Client;

class NetworkController extends Controller
{
    public function __construct( private Client $client ) {}

    public function interfaces() : array
    {
        return $this->client->query( '/interface/print' );
    }
}
bash
php artisan vendor:publish --provider="Ocolin\RouterOS\Laravel\ServiceProvider"