Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search by mac & read protected object #38

Open
brsnik opened this issue Jun 8, 2018 · 4 comments
Open

Search by mac & read protected object #38

brsnik opened this issue Jun 8, 2018 · 4 comments
Labels

Comments

@brsnik
Copy link

brsnik commented Jun 8, 2018

Hi,

When I print all the leases:
$array = $util->setMenu('/ip dhcp-server lease')->getAll();

I get an object like this, how can I read this:

object(PEAR2\Net\RouterOS\ResponseCollection)#276 (8) {
  ["responses":protected]=>
  array(263) {
    [0]=>
    object(PEAR2\Net\RouterOS\Response)#10 (4) {
      ["unrecognizedWords":protected]=>
      array(0) {
      }
      ["_type":"PEAR2\Net\RouterOS\Response":private]=>
      string(3) "!re"
      ["attributes":protected]=>
      array(14) {
        [".id"]=>
        string(3) "*20"
        ["address"]=>
        string(9) "10.0.0.71"
        ["mac-address"]=>
        string(17) "B0:E8:92:30:C9:4D"
        ["client-id"]=>
        string(19) "1:b0:e8:92:30:c9:4d"
        ["address-lists"]=>
        string(0) ""
        ["server"]=>
        string(21) "dhcp1_private_network"
        ["dhcp-option"]=>
        string(0) ""
        ["status"]=>
        string(7) "waiting"
        ["last-seen"]=>
        string(12) "9w3d1h13m15s"
        ["host-name"]=>
        string(11) "EPSON30C94D"
        ["radius"]=>
        string(5) "false"
        ["dynamic"]=>
        string(5) "false"
        ["blocked"]=>
        string(5) "false"
        ["disabled"]=>
        string(5) "false"
      }
      ["_tag":"PEAR2\Net\RouterOS\Message":private]=>
      NULL
    }

And is it possible to pass a mac address here: '/ip dhcp-server lease', so I will only get all this info for 1 mac address instead of all the clients?

@boenrobot
Copy link
Member

boenrobot commented Jun 9, 2018

You should check that page in the wiki that says "API Reference".

It has detailed information on all methods and classes in the package. In this case, you're asking about Util::getAll() and ResponseCollection.

To answer your specific questions though, you can read a ResponseCollection as if you would read an array - a foreach loop, or a for loop with a count(), and as you can see in the reference, there are also a few extra methods for filtering it and sorting the responses.

Each response in the ResponseCollection, you can also read as if you were reading an associative array (for properties), and there are a few extra methods for accessing things that are not properties (type and tag). So in its simplest form, you can think of a ResponseCollection as an array of associative arrays. e.g.

foreach ($array as $response) {
    foreach ($response as $propName => $propValue) {
        echo 'The property "' . $propName . '" has a value "' . $propValue . '". ';
    }
}

And yes, just as you can use a "where" from a command line, you can use a RouterOS\Query object as the second argument of getAll() to return only items matching a criteria, in this case, all where the MAC address is a certain one, f.e.

$array = $util->setMenu('/ip dhcp-server lease')
    ->getAll(array(), RouterOS\Query::where('mac-address', 'B0:E8:92:30:C9:4D'));

As you can see in the reference, the first argument is extra arguments to pass to the "print" request.

@brsnik
Copy link
Author

brsnik commented Jun 12, 2018

On our company router we have two networks, public and private.

I've connected to both for testing purposes. And when I search by MAC I get the following error:

Fatal error:  Uncaught PEAR2\Net\RouterOS\RouterErrorException: Error getting property in /var/www/.../PEAR2/Net/RouterOS/Util.php:561
Stack trace:
#0 /var/www/.../mtk_test.php(68): PEAR2\Net\RouterOS\Util->get('*2f32*2f57')
#1 {main}
Response collection:
Array
(
    [0] => PEAR2\Net\RouterOS\Response Object
        (
            [unrecognizedWords:protected] => Array
                (
                )

            [_type:PEAR2\Net\RouterOS\Response:private] => !trap
            [attributes:protected] => Array
                (
                    [message] => invalid internal item number
                )

            [_tag:PEAR2\Net\RouterOS\Message:private] => 
        )

    [1] => PEAR2\Net\RouterOS\Response Object
        (
            [unrecognizedWords:protected] => Array
                (
                )

            [_type:PEAR2\Net\RouterOS\Response:private] => !done
            [attributes:protected] => Array
                (
         in /var/www/.../PEAR2/Net/RouterOS/Util.php on line 561

Totally crashes my script.

Is it possible for catch{} to return the message [message] => invalid internal item number?

@boenrobot
Copy link
Member

First off, assuming you used Util::find() with a Query (and you then passed the result to Util::get()), the cause is probably this issue. I'll release a new fixed version soon, though if you want it "now", you can use Composer with "dev-develop" as the version of "pear2/net_routeros" to get.

But in general, yes, as the error message itself says, this is an uncaught exception, meaning you can catch it if you want, and deal with it, f.e.

try {
$address = $util->get($util->find(RouterOS\Query::where('mac-address', 'B0:E8:92:30:C9:4D')), 'address');
} catch (RouterOS\RouterErrorException $e) {
echo 'Router message: ' . $e->getResponses()->getAllOfType(RouterOS\Response::TYPE_ERROR)->getProperty('message');
}

@brsnik
Copy link
Author

brsnik commented Jun 14, 2018

Thanks.

Error message is returned this way.
However theres a Notice you might want to look into for the next update.

Notice: Array to string conversion in /var/www/.../PEAR2/Net/RouterOS/Util.php on line 488

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants