Community discussions

MikroTik App
 
tnt2
just joined
Topic Author
Posts: 19
Joined: Wed Jun 23, 2010 2:47 pm

quickly delete address list via API

Sun Nov 29, 2015 6:33 pm

hi,

is it possible runnning this command via API ?

ip firewall address-list remove [/ip firewall address-list find list=list_1 ]

this command from cli delete address list quickly.

i try, without success:
$API->write('/ip/firewall/address-list/remove',false);
$API->write('[/ip/firewall/address-list/print]',false);
$API->write('?list=' . $address_list,true);


now i read address list =.proplist=.id and then delete in cycle.
This is long enough.

i use php api 1.6

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

Re: quickly delete address list via API

Sun Nov 29, 2015 7:05 pm

Last I checked, API's "find" command doesn't support queries. It can only give you the IDs of all items at a menu. Only "print" supports queries.

So yeah, the only way currently is to first do a "print" API call with ".proplist=.id", loop through the results, gather them into a single comma separated list, and pass that list into the "numbers" argument of another API call to the "remove" command.

With my API client, the above is automated into a single PHP method call, but that's what happens under the hood.
 
tnt2
just joined
Topic Author
Posts: 19
Joined: Wed Jun 23, 2010 2:47 pm

Re: quickly delete address list via API

Sun Nov 29, 2015 7:41 pm

loop through the results, gather them into a single comma separated list, and pass that list into the "numbers" argument of another API call to the "remove" command.
please give me example for this comma separated list and how to write this via API ?
now in cycle, i doing this:
$API->write('/ip/firewall/address-list/remove',false);
$API->write('=.id='. $id ,true);
$READ = $API->read(false);

it is possible delete all items in addr. list with one API->write command ?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: quickly delete address list via API

Sun Nov 29, 2015 7:49 pm

it is possible delete all items in addr. list with one API->write command ?
No.
please give me example for this comma separated list and how to write this via API ?
The basic idea is that you have a single string you append to, and strip the leading/trailing comma in the end. e.g.:
$idList = '';
foreach (...) {
    ...
    $idList .= ',' . $id;
}
$idList = substr($idList, 1);
In the end, $idList contains the comma separated list you'd give to "remove".
 
tnt2
just joined
Topic Author
Posts: 19
Joined: Wed Jun 23, 2010 2:47 pm

Re: quickly delete address list via API

Sun Nov 29, 2015 10:01 pm

thanks. it works.


i add to address list with this commnad:
$ARRAY = $API->comm('/ip/firewall/address-list/add', array(
"address" => "1.1.1.20",
"disabled" => "no",
"list" => "test1",
"comment" => "comment1",
));

is it possible add more than 1 record in $API->comm ...
same as multiple/multidimensional array ?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: quickly delete address list via API

Sun Nov 29, 2015 10:06 pm

No. There's no way even on the command line, and thus no way in the API.

Again, with my own API client, there's a way to add multiple items with one PHP method call, but under the hood, that gets translated to multiple consecutive "add" calls.
 
tnt2
just joined
Topic Author
Posts: 19
Joined: Wed Jun 23, 2010 2:47 pm

Re: quickly delete address list via API

Sun Nov 29, 2015 10:38 pm

Thank you very much for your advice!

sometime i will try your API client
 
gabrieled209
just joined
Posts: 11
Joined: Thu Apr 18, 2013 6:26 pm

Re: quickly delete address list via API

Mon Jun 14, 2021 11:48 pm

you can remove quickly using ssh access instead of api

ex:
function sshExecute($ip,$username,$password,$comandos) {
$comm = implode(';', $comandos);
$ssh = new \phpseclib\Net\SSH2($ip); <- i use phpseclib library: https://github.com/phpseclib/phpseclib
if (!$ssh->login($username, $password)) {
throw new Exception('No se pudo loguear');
}
$ssh->exec(":execute {".$comm."}");
}
sshExecute([IP],[user],[pass],['/ip firewall address-list remove [/ip firewall address-list find list=list_1 ]']);

Who is online

Users browsing this forum: No registered users and 27 guests