Community discussions

MikroTik App
 
netboyzin
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Thu Mar 21, 2013 3:42 pm

How to get the Flag value

Thu Jun 04, 2015 12:30 pm

Hi All
How to filter out just the Flag Value ( whether an element is disabled or enabled ) by script.

Any help is welcome.

Abhishek
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to get the Flag value

Thu Jun 04, 2015 12:35 pm

You can use
:local disabledItems [print as-value where disabled=yes];
:local enabledItems [print as-value where disabled=no];
to get all enabled/disabled items, and then loop over those with :foreach. Or if you want to just target them for something else (e.g. removal), you can use "find" instead of "print as-value".

The key point is the "where disabled=?" part, with "yes" targeting disabled items, and "no" targeting enabled items.


If you want to get the actual state of a particular item, simply use [get name-or-id-of-item disabled], and you'll get "yes" or "no" returned as a string.
 
netboyzin
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Thu Mar 21, 2013 3:42 pm

Re: How to get the Flag value

Thu Jun 04, 2015 2:03 pm

Hi
Thanks for your reply.

We need to get the flag value depending upon which we need to take decision.

If IP == XX.XX.XX.XX && flag ==disabled { do this } else { do that } in a PHP code.

IP is supplied by user but how to get the flag value ?

Method 2
-------------
Otherwise how to check the status of output of a command - if a condition is met a command returns a result otherwise it returns nothing , so how to check the if the condition is met and the command returned some value ?

Abhishek
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to get the Flag value

Thu Jun 04, 2015 2:21 pm

You could do
:foreach item in=[print as-value] do={
    :if ("yes" = ($item->"disabled") and "XX.XX.XX.XX" = ($item->"address")) do={
        #IP is disabled
    } else={
        #Another IP, or the IP is enabled
    }
}
assuming we're talking about the "/ip arp" menu or the like.
 
netboyzin
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Thu Mar 21, 2013 3:42 pm

Re: How to get the Flag value

Thu Jun 04, 2015 2:59 pm

I am working in /ip hotsopt ip-binding menu , I think using foreach is not required here.

Abhishek
 
netboyzin
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Thu Mar 21, 2013 3:42 pm

Re: How to get the Flag value

Thu Jun 04, 2015 3:10 pm

How to embed the above code in PHP ?

Abhishek
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to get the Flag value

Thu Jun 04, 2015 4:48 pm

I think using foreach is not required here.
Depends on what you want to do with the matches.

If you want to, f.e. remove all disabled ones, then yes, using foreach is not needed - you simply use the "find" command to get the IDs, and pass that to a remove command.

If you want to do something more complicated though, like adding them to a database, you'll need to loop over the results to get the remaining properties of each matching item.
How to embed the above code in PHP ?
Using the API protocol.

With the client from my signature, the equivalent of the above would be like:
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('router-ip', 'admin', 'password'));
$util->setMenu('/ip hotsopt ip-binding');

foreach ($util->getAll() as $item) {
    if ('yes' === $item('disabled') && 'XX.XX.XX.XX' === $item('address')) {
        //IP is disabled
    } else {
        //Another IP, or IP not disabled
    }
} 
Or perhaps if you want to remove all matches:
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('router-ip', 'admin', 'password'));
$util->setMenu('/ip hotsopt ip-binding')->remove(
    $util->find(
        RouterOS\Query::where('disabled', 'yes')->andWhere('address', 'XX.XX.XX.XX')
    )
);