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/ */
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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.