System Print PEAR2 API

Im currently trying to get the router to display the results of /system resource print.

It works fine but if one of the ‘wan_ips’ in my table is offline non of the IPs after it run the script?

is there a way to allow the script to bypass failed IPs? or even just echo ‘OFFLINE’;?

as always help is much appreciated.

$client = new RouterOS\Client(‘$row[‘wan_ip’]’, ‘admin’, ‘password’, 8101);

$responses = $client->sendSync(new RouterOS\Request(‘/system/resource/print’));
echo $wan_location . ': ';
foreach ($responses as $response) {
if ($response->getType() === RouterOS\Response::TYPE_DATA) {
echo 'Uptime: ', $response->getArgument(‘uptime’),
’ CPU Load: ', $response->getArgument(‘cpu-load’), ‘%’, ‘
’, “\n”;
}
}

A failed connection would throw an exception. Simply catch it, and do whatever you want on failed connections at the catch block, e.g.

try {
    $client = new RouterOS\Client($row['wan_ip'], 'admin', 'password', 8101);

    $responses = $client->sendSync(new RouterOS\Request('/system/resource/print'));
    echo $wan_location . ': ';
    foreach ($responses as $response) {
        if ($response->getType() === RouterOS\Response::TYPE_DATA) {
            echo 'Uptime: ', $response->getArgument('uptime'),
                ' CPU Load: ', $response->getArgument('cpu-load'), '%', '<br>', "\n";
        }
    }
} catch (Exception $e) {
//Do whatever you want to happen on connection failure here, or do nothing to simply skip it
} 

(P.S. You should also remove the apostrophes around the first argument or the client constructor, as shown in the example above.)

worked great, the peal client is working really well. I’m currently trying to get the rx-bytes and tx-bytes from ether0 any tips on this?

currently have this and its giving me values for all the ports not just ether0 any ideas?

$result = $client(new RouterOS\Request(‘/interface/print stats=where=name=“ether1-gateway”’))->getAllOfType(RouterOS\Response::TYPE_DATA);
foreach ($result as $entry) {
$name = $entry(‘name’);

$down = $entry(‘rx-byte’); //rx-bytes = down data
$up = $entry(‘tx-byte’); //tx-bytes = updata

echo 'Name: ', $name;
echo ‘
’;
echo 'UP: ', $up;
echo ‘
’;
echo 'DOWN: ', $down;
echo ‘
’;
//…
}

The “where” argument is not available from the API protocol. You must use queries instead. The easiest way is as the second argument of the request constructor, e.g.

new RouterOS\Request('/interface/print stats=""', RouterOS\Query::where('name', 'ether1-gateway')) 

(assuming you wanted data for an interface named “ether1-gateway”; adjust accordingly for “ether0”)

the name is variable though? so might differ depending on router is there a rule to just choose port 0?

is there a rule to just choose port 0?

Sort of… you can make an initial (normal) print request for ALL interfaces. They’re all printed in order*, so after you get the name of the first one, you can supply it to any further operations. The effect will be as targeting by a number.

An upcoming update of the client has an abstraction for such functionality (i.e. you call a single function which does that very thing), but for now - that’s the way to duplicate it.

  • It at least appears so currently… MikroTik support hasn’t confirmed or denied whether this can be relied upon, despite me explicitly asking the question. If you want to err on the side of caution, you best have a DB where you store not only the IPs and credentials, but also the interface names of all routers involved.

some places do accept item name as .id value, like interface names. That has been written here in the forums several times by me. As that is not the case for all possible menus numeric (*) identifier use is encouraged as that will be homogenous through out the API.