PHP code example of srikanta159 / net_routeros_php

1. Go to this page and download the library: Download srikanta159/net_routeros_php 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/ */

    

srikanta159 / net_routeros_php example snippets






EAR2\Net\RouterOS\Client;
use PEAR2\Net\RouterOS\Request;
use PEAR2\Net\RouterOS\Response;

try {
    // Connect to RouterOS (v6 or v7)
    $client = new Client('192.168.88.1', 'admin', 'your_password');

    // Send command
    $request = new Request('/system/resource/print');
    $responses = $client->sendSync($request);

    foreach ($responses as $response) {
        if ($response->getType() === Response::TYPE_DATA) {
            echo "Version:    " . $response->getProperty('version') . "\n";
            echo "CPU Load:   " . $response->getProperty('cpu-load') . "%\n";
            echo "Free Memory: " . round($response->getProperty('free-memory') / 1024 / 1024, 2) . " MB\n";
            echo "Uptime:     " . $response->getProperty('uptime') . "\n";
        }
    }

    $client->close();
} catch (\Throwable $e) {
    echo "Connection error: " . $e->getMessage() . "\n";
}



use PEAR2\Net\RouterOS\Client;
use PEAR2\Net\RouterOS\Request;
use PEAR2\Net\Transmitter\NetworkStream;

// Connect over TLS on port 8729
$client = new Client(
    '192.168.88.1',
    'admin',
    'your_password',
    8729,
    false,
    null,
    NetworkStream::CRYPTO_TLS
);

$request = new Request('/interface/print');
$responses = $client->sendSync($request);

foreach ($responses->getAllOfType(PEAR2\Net\RouterOS\Response::TYPE_DATA) as $iface) {
    echo "Interface: " . $iface->getProperty('name') . " (" . $iface->getProperty('type') . ")\n";
}

$client->close();



use PEAR2\Net\RouterOS\Client;
use PEAR2\Net\RouterOS\Request;
use PEAR2\Net\RouterOS\Query;
use PEAR2\Net\RouterOS\Response;

$client = new Client('192.168.88.1', 'admin', 'password');

// Query: Find ethernet interfaces that are running
$query = Query::where('type', 'ether', Query::OP_EQ)
    ->andWhere('running', 'true', Query::OP_EQ);

$request = new Request('/interface/print');
$request->setQuery($query);

$responses = $client->sendSync($request);

foreach ($responses->getAllOfType(Response::TYPE_DATA) as $response) {
    echo "Running Interface: " . $response->getProperty('name') . "\n";
}

$client->close();



use PEAR2\Net\RouterOS\Client;
use PEAR2\Net\RouterOS\Request;
use PEAR2\Net\RouterOS\Response;

$client = new Client('192.168.88.1', 'admin', 'password');

// If a query matches zero rows in RouterOS v7, a !empty response is returned
$request = new Request('/ip/firewall/nat/print');
$request->setQuery(PEAR2\Net\RouterOS\Query::where('comment', 'nonexistent_rule', PEAR2\Net\RouterOS\Query::OP_EQ));

$responses = $client->sendSync($request);

if ($responses->getAllOfType(Response::TYPE_EMPTY)->count() > 0) {
    echo "No matching NAT rules found (RouterOS v7 !empty received).\n";
}

$client->close();

   PHP Fatal error: Uncaught PEAR2\Net\RouterOS\UnexpectedValueException: Unrecognized response type in Response.php
   
bash
composer 
bash
php tests/run_tests.php