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

Get Flag value and PPPOE conenction data #21

Closed
etiennesw opened this issue Jan 28, 2016 · 4 comments
Closed

Get Flag value and PPPOE conenction data #21

etiennesw opened this issue Jan 28, 2016 · 4 comments
Labels

Comments

@etiennesw
Copy link

QUESTION 1

Hi,

I am attempting retrieve the flag data of an address-list entry to check whether it is disabled or enabled, however none of the following worked.

$printRequest = new RouterOS\Request('/ip firewall address-list print');
$printRequest->setArgument('.list');
$printRequest->setQuery(RouterOS\Query::where('disabled', 'yes'));
echo $name = $client->sendSync($printRequest)->getProperty('.list');

or this

$util->setMenu('/ip firewall address-list');
foreach ($util->getAll() as $item) {
if ('yes' ===$item('disabled')){
echo $item->getProperty('list');
} else {
echo $item->getProperty('list');
}
}

When I use as-value on the console nothing is reported, however, when I use value-list, I get the disabled items I need.

/ip firewall address-list> print value-list where disabled=yes
list: Family Android All
address: 192.168.1.91 192.168.1.94 192.168.1.90-192.168.1.119
dynamic: no no no

/ip firewall address-list> print as-value where disabled=yes
nothing


QUESTION 2

Also tried to retrieve data about my PPPOE internet connection:
/ip firewall address-list> /interface pppoe-client monitor pppoe-out1 once
status: connected
uptime: 22h42m58s
active-links: 1
encoding:
service-name:
ac-name: bsjn1
ac-mac: 28:94:0F:8E:26:00
mtu: 1480
mru: 1492
local-address: xxx.xxx.xxx.xxx

with this script, however, I get an empty page

$responses = $client->sendSync(new RouterOS\Request('/interface pppoe-client monitor pppoe-out1 once'));
foreach ($responses->getType() as $item){
echo 'STATUS: ', $item->getProperty('status'),
"\n";
}

@boenrobot
Copy link
Member

With the API protocol, "yes" and "no" values are replaced with "true" and "false" on most places, which is why neither of your checks work.

Also, there's no ".list" property or argument, only "list". If you want to limit the responses to show only the value of "list", use the argument ".proplist" with a value of "list" instead.

So, to get the list names of all disabled items, you can use:

$util->setMenu('/ip firewall address-list');
foreach ($util->getAll(array('.proplist' => 'list'), RouterOS\Query::where('disabled', 'true')) as $item) {
    echo $item->getProperty('list');
}

Unnamed arguments are not supported, since there's no way they can be reliably guessed. In console, if you type

/interface pppoe-client monitor ?

you'll see the output

<numbers> -- List of item numbers
append -- 
as-value -- 
do -- Execute given script after each time it prints stats on the screen
file -- 
interval -- Refresh information in selected time interval
once -- 
without-paging -- 

which tells you that the first unnamed argument is actually called "numbers".

In both console and in API, you can write:

/interface pppoe-client monitor numbers=pppoe-out1 once

which in console is equivalent to what you have.

So:

$responses = $client->sendSync(new RouterOS\Request('/interface pppoe-client monitor numbers=pppoe-out1 once'));
foreach ($responses->getType() as $item) {
    echo 'STATUS: ', $item->getProperty('status'),
    "\n";
}

@etiennesw
Copy link
Author

etiennesw commented Feb 1, 2016

I still didn't manage to get the PPPOE status to work. The getProperty doesn't seem to match any of the fields. I always get a blank page.
As you can see the the var_dump, the details for status and uptime are there, in the array. I just am getting confused how to extract the ones I need.

Actually I am still learning php, therefore, I am not really understanding what the var_dump output means in relation to the API client.

Here is the code I am using and a var_dump result

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';
$client = new RouterOS\Client('192.168.1.1', 'user', 'password');
$responses = $client->sendSync(new RouterOS\Request('/interface pppoe-client monitor numbers=pppoe-out1 once'));
foreach ($responses->getType() as $item){
    echo 'Status :', $item->getProperty('status');
}
?>

VAR_DUMP:

object(PEAR2\Net\RouterOS\ResponseCollection)#7 (8) { ["responses":protected]=> array(2) { [0]=> object(PEAR2\Net\RouterOS\Response)#4 (4) { ["unrecognizedWords":protected]=> array(0) { } ["_type":"PEAR2\Net\RouterOS\Response":private]=> string(3) "!re" ["attributes":protected]=> array(11) _**{ ["status"]=> string(9) "connected" ["uptime"]=> string(10) "1d15h47m1s"_ **["active-links"]=> string(1) "1" ["encoding"]=> string(0) "" ["service-name"]=> string(0) "" ["ac-name"]=> string(5) "bsjn1" ["ac-mac"]=> string(17) "28:94:0F:8E:26:00" ["mtu"]=> string(4) "1480" ["mru"]=> string(4) "1492" ["local-address"]=> string(15) "XXX.XXX.XXX.XXX" ["remote-address"]=> string(15) "XXX.XXX.XXX.XXX" } ["_tag":"PEAR2\Net\RouterOS\Message":private]=> NULL } [1]=> object(PEAR2\Net\RouterOS\Response)#5 (4) { ["unrecognizedWords":protected]=> array(0) { } ["_type":"PEAR2\Net\RouterOS\Response":private]=> string(5) "!done" ["attributes":protected]=> array(0) { } ["_tag":"PEAR2\Net\RouterOS\Message":private]=> NULL } } ["responseTypes":protected]=> array(2) { [0]=> string(3) "!re" [1]=> string(5) "!done" } ["responseTags":protected]=> array(2) { [0]=> NULL [1]=> NULL } ["responsesIndex":protected]=> array(0) { } ["propertyMap":protected]=> NULL ["position":protected]=> int(0) ["index":protected]=> NULL ["compareBy":protected]=> array(0) { } }

Thanks once again for you expertise and help.

@boenrobot
Copy link
Member

Oh, silly me. I didn't noticed your other mistake... the usage of $responses->getType(). As written, this will show you the type (!re, !done, etc.) of the first response (in your case !re) so when you pass that into a foreach, you're looping over that string... Which I'm not sure if it's allowed at all, but if it is, you're looping over string characters.

Since you're using "once", you can just use getProperty() on the $responses, and you'll get those in the first and only response from the router, i.e.

$responses->getProperty('status')

Otherwise, just loop over the responses, not the type, i.e.

foreach ($responses as $item) {
    echo 'Status :', $item->getProperty('status');
}

@etiennesw
Copy link
Author

Thanks. That worked perfectly.

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