Community discussions

MikroTik App
 
veezy
just joined
Topic Author
Posts: 1
Joined: Tue Jul 27, 2010 6:37 pm

API address-list add or remove

Tue Jul 27, 2010 7:08 pm

Hi all,

with this API command i send ip to address-list to my router
/ip/firewall/address-list/add =list=kesik =address=1.2.3.4 =disabled=no
and i cant remove it with API command.

Can any one help me please about send this command with api?

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



i try lots of time but i cant do it.

thanks.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: API address-list add or remove

Wed Jul 28, 2010 8:58 pm

/ip firewall address-list remove [/ip firewall address-list find address=1.2.3.4]
there are two commands (find, then remove). so, you should first execute
/ip/firewall/address-list/print
?address=1.2.3.4
=.proplist=.id
and then use that .id to remove the entry:
/ip/firewall/address-list/remove
=.id=<result_of_previous_query>
 
strg
Member Candidate
Member Candidate
Posts: 101
Joined: Sun Mar 07, 2010 4:01 am

Re: API address-list add or remove

Tue Nov 29, 2011 4:43 pm

and for the command below ?

/ip firewall address-list remove [/ip firewall address-list find list=ssh-accept]
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: API address-list add or remove

Tue Nov 29, 2011 6:01 pm

let me guess... just replace
?address=1.2.3.4
with
?list=ssh-accept
? :D
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API address-list add or remove

Tue Nov 29, 2011 7:16 pm

If there is more than one entry, you can include all IDs by separating them with a comma. I don't know if =.id= supports this, but last I checked, =numbers= does.
 
strg
Member Candidate
Member Candidate
Posts: 101
Joined: Sun Mar 07, 2010 4:01 am

Re: API address-list add or remove

Tue Nov 29, 2011 7:43 pm

i receive the error : !trap=message=unkown parameter

My code is:
            mikrotik.Send("/ip/firewall/address-list/print");
            mikrotik.Send("?list=ssh-accept");
            mikrotik.Send("=.proplist=.id");

            mikrotik.Send("/ip/firewall/address-list/remove");
            mikrotik.Send("=.id=");

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

Re: API address-list add or remove

Tue Nov 29, 2011 9:02 pm

You need to actually store the ID into a variable (by using the Read() method, and analyze the returned data appropriately), and then concatenate the returned ID as part of the next command. What you're instead doing is just sending a print command, following by a remove command that misses the actual ID to remove.

That, plus the C# client in the wiki is... ergh... have you considered using mikrotik4net (the C# client in the "elsewhere" section)?
 
stimels
newbie
Posts: 26
Joined: Sun Jan 31, 2010 9:56 pm

Re: API address-list add or remove

Sun Sep 23, 2012 8:44 am

hm....
i have array with ip, which need to remove from address list
$ros_list=mylist;
foreach ($removeiparray as $remove)
{
echo "Remove IP: " . $remove . "\n";

$API->write("/ip/firewall/address-list/print",false);
   $API->write("?list=$ros_list",false);
   $API->write("?address=$remove",false);
   $API->write("=.proplist=.id");
   $READ = $API->read();
   $i=0;
   foreach   ($READ as $x) {
           $line=$READ[$i];
           $id=$line['.id'];
           sleep(1);
           $API->write("/ip/firewall/address-list/remove",false);
           $API->write("=.id=$id",true);
           $i++;
#           usleep(5000);
}}}
i create ip - 2.2.2.2
in debug i see:
We removing next ip: 2.2.2.2
<<< [31] /ip/firewall/address-list/print
<<< [14] ?list=mylist
<<< [16] ?address=2.2.2.2
<<< [14] =.proplist=.id
>>> [3/3] bytes read.
>>> [3, 19]!re
>>> [10/10] bytes read.
>>> [10, 8]=.id=*1ED6
>>> [5/5] bytes read.
>>> [5, 1]!done
<<< [32] /ip/firewall/address-list/remove
<<< [10] =.id=*1ED6
Disconnected...
But he still stay in address-list
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API address-list add or remove

Sun Sep 23, 2012 11:50 am

I got a similar bug report about my client... It seems despite what the API spec says (that processing starts when the end of a sentence is received), processing actually only starts when RouterOS is about to send you back a response. But unless you actually demand such a response, i.e. if you just disconnect, RouterOS is not going to do a thing.

To fix this, simply do a read() after the write, i.e.
           $API->write("/ip/firewall/address-list/remove",false);
           $API->write("=.id=$id",true);
           $API->read();
           $i++; 

BTW, it will be SIGNIFICANTLY more efficient to get all the IDs with a single print, and then pass all IDs (separated with commas) to the remove command. And what's with all the sleeps? Are you trying to make the code slow?
 
stimels
newbie
Posts: 26
Joined: Sun Jan 31, 2010 9:56 pm

Re: API address-list add or remove

Sun Sep 23, 2012 11:59 am

I got a similar bug report about my client... It seems despite what the API spec says (that processing starts when the end of a sentence is received), processing actually only starts when RouterOS is about to send you back a response. But unless you actually demand such a response, i.e. if you just disconnect, RouterOS is not going to do a thing.

To fix this, simply do a read() after the write, i.e.
           $API->write("/ip/firewall/address-list/remove",false);
           $API->write("=.id=$id",true);
           $API->read();
           $i++; 

BTW, it will be SIGNIFICANTLY more efficient to get all the IDs with a single print, and then pass all IDs (separated with commas) to the remove command. And what's with all the sleeps? Are you trying to make the code slow?
thanks, i inserted  $API->read();
code is work

how can i optimize code ?
if (count($removeiparray) > 0)
{
foreach ($removeiparray as $remove)
{
echo "We removing next ip: " . $remove . "\n";

$API->write("/ip/firewall/address-list/print",false);
   $API->write("?list=$ros_list",false);
   $API->write("?address=$remove",false);
   $API->write("=.proplist=.id");
   $READ = $API->read();
   $i=0;
   foreach   ($READ as $x) {
           $line=$READ[$i];
           $id=$line['.id'];
           sleep(1);

        $API->write("/ip/firewall/address-list/remove",false);
        $API->write("=.id=$id",true);
        $API->read();
        $i++;
}}}
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API address-list add or remove

Sun Sep 23, 2012 12:23 pm

May I first ask why you chose that client?*

Using my client (see my signature), you can do it like that (and if you do measurements, you'll also see it's a lot more efficient):
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

$client = new Client('192.168.0.1', 'admin', 'password');

$responses = $client->sendSync(
    new Request('/ip/firewall/address-list/print .proplist=.id', Query::where('list', 'mylist'))
)->getAllOfType(Response::TYPE_DATA);

$idList = '';
foreach ($responses as $response) {
    $idList .= $response->getArgument('.id') . ',';
}

if ('' !== $idList) {
    $removeRequest = new Request('/ip/firewall/address-list/remove');
    $removeRequest->setArgument('numbers', rtrim($idList, ','));
    $client->sendSync($removeRequest);
} 
It's the same idea with Denis' client.

(the code above can be shortened further - especially if you have PHP 5.4 - but I'm keeping it verbose for clarity's sake)

* No. Honestly... I'm not criticizing or anything, I'm genuinely curious. Every time I ask this question, people either insist on using Denis' class without giving a reason or switch to my client without saying why they previously used the other one, leaving me to wonder what I'm doing wrong in terms of "PR", even if "technically" it's all good.
 
stimels
newbie
Posts: 26
Joined: Sun Jan 31, 2010 9:56 pm

Re: API address-list add or remove

Sun Sep 23, 2012 3:17 pm

i used this client API:
http://wiki.mikrotik.com/wiki/API_PHP_class

because i don't work with pear :)
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API address-list add or remove

Sun Sep 23, 2012 3:31 pm

Ahhh... *facepalm*... I suspected that much...

Despite the name, PEAR(2) is not required. It's not even an optional dependency in fact. Everything that is needed is included in the PHAR file which you can just include directly, as in the example above. In other words, you don't need to work with PEAR to use this.


edit:

OK... I wrote MikroTik support to shave off the "using PEAR2" part, as that's just misleading...
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: API address-list add or remove

Mon Sep 24, 2012 11:46 am

API states, that each successfully sent command will have a reply to it on the execution results. So generally it is a good idea to get the response if everything happened or not (!re or !trap in response)
 
stimels
newbie
Posts: 26
Joined: Sun Jan 31, 2010 9:56 pm

Re: API address-list add or remove

Mon Sep 24, 2012 5:55 pm

ok, i will try it
 
clarkritchie
just joined
Posts: 3
Joined: Fri Apr 10, 2015 9:31 pm

Re: API address-list add or remove

Fri Apr 10, 2015 9:40 pm

I'm trying to do something similar using Python. However, the .id that I get back is not a decimal number, and I can't pass it on to the remove command.

I want to kick PPPoE user #0:
[admin@BOMA_lab] > /ppp active print
Flags: R - radius
 #   NAME         SERVICE CALLER-ID         ADDRESS         UPTIME   ENCODING
 0 R andristhe... pppoe   D4:CA:6D:04:C0:14 0.0.0.0         18m40s
Here is what the API returns to me:
<<< /ppp/active/print
<<< =.proplist=.id
<<<
>>> !re
>>> =.id=*80000010
I can't seem to do much with *80000010, e.g. /interface/pppoe-server/remove=.id=*80000010 fails. /interface/pppoe-server/remove=.id=80000010 fails, etc.

*80000010 looks like a pointer to memory location to me, it doesn't convert into a usable base 10 number.

Am I missing something obvious here? Thanks. This is router OS 6.20.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: API address-list add or remove

Mon Apr 13, 2015 12:53 pm

if you get .id from /ppp/active, then you must use it under /ppp/active, not /interface/ or something

the correct command is:
/ppp/active/remove
=.id=*80000010
 
Raelkbg
just joined
Posts: 1
Joined: Thu Apr 12, 2018 12:18 pm

Re: API address-list add or remove

Thu Apr 12, 2018 12:24 pm

Hi

I need help with the command to reset-counters. this is my code in c#:

var cmd= connection.CreateCommandAndParameters("/tool/user-manager/user/reset-counters ", "?=numbers", txtreset.Text);

I tried using .id instead of numbers unfortunately I keep getting the same error message "no such command"
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: API address-list add or remove

Fri Apr 13, 2018 6:10 pm

I keep getting the same error message "no such command"
That means, "/tool/user-manager/user/reset-counters " command does not exist. Try without space symbol at the end of string ("...ters" instead of your "...ters ").
 
u1b2n3t41520
just joined
Posts: 1
Joined: Tue Feb 25, 2020 5:45 am

Re: API address-list add or remove

Tue Feb 25, 2020 5:55 am

this works to me:
<?php 
    try {
        $client = new RouterOS\Client('192.168.88.1', 'admin', 'password');
    } catch (Exception $e) {
        echo "error : ".$e;
        die('Unable to connect to the router.');
        
    }
///ip firewall address-list remove [/ip firewall address-list find address=192.168.88.14]
$printRequest = new RouterOS\Request('/ip/firewall/address-list/print');
$printRequest->setArgument('.proplist', '.id');
$printRequest->setQuery(RouterOS\Query::where('address', '192.168.88.14'));
$id = $client->sendSync($printRequest)->getProperty('.id');
//$id now contains the ID of the entry we're targeting
$setRequest = new RouterOS\Request('/ip/firewall/address-list/remove');
$setRequest->setArgument('numbers', $id);
$client->sendSync($setRequest);
?>
 
netbyte
just joined
Posts: 1
Joined: Tue Jun 22, 2021 7:32 am

Re: API address-list add or remove

Tue Jun 22, 2021 7:36 am

PHP Code
include '../includes/mt.php';

$ServerList ="10.10.69.6"; //ip_de_tu_API
$Username ="TheUser"; //usuario_API
$Pass ="TheMagicKey"; //contraseña_API
$port ="8728"; //puerto_API
$target = "172.17.50.39";
$comment = "deudor cortado $target ";

$API = new routeros_api();
$API->debug = false;

if ($API->connect($ServerList, $Username, $Pass, $port)) {
 $API->write("/ip/firewall/address-list/getall",false);
  $API->write('?address='.$target,true);
  $READ = $API->read(false);
  $ARRAY = $API->parseResponse($READ);
   if(count($ARRAY)>0){
echo "Error: la IP afectada ya existe desde el $ARRAY[0] .  No se hizo nada nuevo.";
   }else{
       $API->write("/ip/firewall/address-list/add",false);
       $API->write('=address='.$target,false);   // IP
       $API->write('=disabled=yes',false);
       $API->write('=list=deudores',false);
       $API->write('=comment='.$comment,true);         // comentario
       $READ = $API->read(false);
       $ARRAY = $API->parseResponse($READ);
       echo "la suspension a $target, ha sido creado con exito!.";
   }
   $API->disconnect();
 } else {
  echo "no se ha podido conectar." ;
}
 
avistudio
just joined
Posts: 5
Joined: Sun Apr 28, 2019 9:56 pm

Re: API address-list add or remove

Sat Nov 19, 2022 2:13 pm

Hello! I used to have that question too. After half day of experiments I've done it!
function enable_user_API($AdresaIp){
    require('routeros_api.class.php');
    $API = new Routeros_api();

    if ($API->connect('IP', 'USER', 'PASSWORD')) {
    # Get all current hosts
    $API->write('/ip/firewall/address-list/print');
    $ips = $API->read();
    foreach($ips as $num => $ip_data){
    if($ip_data["list"]=="BlackList" && $ip_data["address"]==$AdresaIp){

        $API->write('/ip/firewall/address-list/remove', false);
        $API->write("=.id=".$ip_data[".id"], true);
        $API->read(false);

        }
    }

    $API->disconnect();
    }
}

Who is online

Users browsing this forum: No registered users and 16 guests