routeros-api-php
routeros-api-php copied to clipboard
Find command doesn't support query
Version of RouterOS Any, or at least any recent.
To Reproduce Create a couple of lists on a mikrotik device with some ips:
/ip firewall address-list
add address=192.168.1.1 list=list1
add address=192.168.1.2 list=list1
add address=10.10.0.1 list=list2
add address=10.10.0.2 list=list2
Then execute this code:
$query = (new Query('/ip/firewall/address-list/find'))
->where('list', 'list1');
$ips = $client->qr($query);
$ips = $ips["after"]["ret"];
var_dump($ips);
Expected behavior In theory you should receive a list of two ip addresses, instead you'll receive back the list of all addresses because the query is simply ignored.
Possible solution You can try something like this:
$query = (new Query('/ip/firewall/address-list/print'))
->equal('.proplist', '.id')
->where('list', 'list1');
$ips = [];
foreach ($client->qr($query) as $item) {
$ips[] = $item['.id'];
}
$ips = implode(";", $ips);
var_dump($list);
Reference Mikrotik Wiki
If you read carefully there's a line saying: Currently only print command handles query words.
So said, the wipe_address_list.php example is misleading since you'll wipe all addresses and not only the ones you actually want to remove!