Remove BGP network by comment via PHP API

Hello everyone,

I’m trying to remove BGP network with comment which contain word “test” via PHP API but without success:
Mikrotik command is:

/routing bgp network  remove [find comment~"test"];

I tried with 2 solution, but still doesn’t work:
1.

 
 $API->write( '/routing/bgp/network/remove', false );
 $API->write( '[find comment~"test"]' );
$API->write( '/routing/bgp/network/print', false );
$API->write('?comment~test', false);
$API->write('=.proplist=.id');
$ARRAYS = $API->read();

$API->write('/routing/bgp/network/remove', false);
$API->write('=.id=' . $ARRAYS[0]['.id']);
$READ = $API->read();

Can someone help.
Thanks!

comment~test is not a valid query.

See the manual for valid queries:
https://wiki.mikrotik.com/wiki/Manual:API#Queries

Hi mrz,

I was try with ?comment=test but result is same. Please can you help me with this.

Key words (contain word “test” ). Queries in API do not have regexp matching, so you have two options either match exact comment string or get all networks and do comment filtering on client side app.

Ok, i will try to add to router script “routing bgp network remove [find comment~“Fast”]” and run this script via PHP API.

Now, problem with runing script via PHP API. Script name is ocisti-bgp-network

$API->write( '/system/script/run', false );
$API->write( '=ocisti-bgp-network', false );

What is wrong there?

I managed to run the script (or just script counter) with:

$API->write('/system/script/run',false);
$API->write("=.id=*4");

However, there appears to be a bug in the Mikrotik ROS? When I run script via PHP API, the script is not executed, only the run count number is increased. When I run manually from a winbox or terminal, then the script is executed ok. How can I solve this?

You may want to check your permissions, or rebuild script and check what line causes it to fail.

This example works fine:

$API->write('/system/script/run',false);
$API->write("=.id=ocisti-bgp-network"); /* You may use script name instead of internal ID */



/system script
add name=ocisti-bgp-network owner=admin policy=test source="/log info test"

Hm, it doesn’t work for me on ROS 6.42.7 when in script is:

/routing bgp network remove [find comment="test"];

or

/routing bgp network remove [find comment~"test"];

Only the count number is increased.

 3   name="ocisti-bgp-network" owner="admin" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon 
     last-started=sep/04/2018 12:38:12 run-count=49 source=/routing bgp network remove [find comment="test"];

This is php script:

#!/usr/bin/php
<?php

//prikazi greske
error_reporting( 0 );
error_reporting( E_ALL );
ini_set( 'display_errors', 'On' );
define( "_VER", '1.0' );

$fecha_now = date("Y-m-d H:i:s", time());

/* KONEKCIJA NA MIKROTIK ROUTER */
$cfg[ ip_mikrotik ] = "1.1.1.1"; // IP adresa routera
$cfg[ api_user ]    = "user"; // User
$cfg[ api_pass ]    = "pass"; // Password

require_once "routeros_api.php";
$API = new RouterosAPI();

$API->connect( $cfg[ ip_mikrotik ], $cfg[ api_user ], $cfg[ api_pass ] );

// Ispisi u logu routera
$comment_rule = 'GlobalHost Guard je restartan i BGP mreze su flushane ' .$fecha_now;
$API->write( '/log/info', false );
$API->write( '=message=' . $comment_rule );

// Pokreni skriptu na routeru

//$API->write('/routing/bgp/network/remove',false);
//$API->write('?=comment=test',false);

$API->write('/system/script/run',false);
$API->write("=.id=ocisti-bgp-network");

$API->read(false);
$API->disconnect();
?>

Maybe a timing issue? Are you sure that the output of $API->read(false); is “!done”?

This is probably Mikrotik bug, there is a lot posts about this problem, and each and everyone is without solution. All I can do is try my first idea, to remove BGP network directly via PHP API.
Can you please help me and tell me what is wrong in this two examples:

$API->write( '/routing/bgp/network/print', false );
$API->write('?comment=test', false);
$API->write('=.proplist=.id');
$ARRAYS = $API->read();

$API->write('/routing/bgp/network/remove', false);
$API->write('=.id=' . $ARRAYS[0]['.id']);
$READ = $API->read();

or

 $API->write( '/routing/bgp/network/remove', false );
 $API->write( '[find comment=tes"]' );

I need to delete from routing bgp network all networks with comment “test”, nothing more, but today is not my day, everything is going wrong :slight_smile:

API and terminal is not the same thing
There is no such thing as ‘[find comment=tes"]’ or regexp matching in API. Read link provided earlier about API queries.

First method does not work, because you do not have bgp network with comment ‘test’, you have comment that contains test

Read possible solution mentioned in my previous post.

HI, i have network with comment “test”, (only test), but first method still doesn’t work.

#   NETWORK              SYNCHRONIZE
 0   ;;; test
     2.2.2.2/32           no



$API->write( '/routing/bgp/network/print', false );
$API->write('?comment=test', false);
$API->write('=.proplist=.id');
$ARRAYS = $API->read();

$API->write('/routing/bgp/network/remove', false);
$API->write('=.id=');
$READ = $API->read();

You haven’t specified any ID to remove.

I only want to remove all networks with comment “test”, nothing more, ID is not always same so can’t define ID.
That should be simple with code from below, but it’s not working.

$API->write('/routing/bgp/network/remove',false);
$API->write('?=comment=test');

It does not change in milliseconds (time between two commands)

Works as expected:

/routing/bgp/network/print 
?comment=test
=.proplist=.id

<<< /routing/bgp/network/print
<<< ?comment=test
<<< =.proplist=.id
<<< 
>>> !re
>>> =.id=*0
>>> 
>>> !done
>>> 

/routing/bgp/network/remove
=.id=*0

<<< /routing/bgp/network/remove
<<< =.id=*0
<<< 
>>> !done
>>>

mrz, do you know why doesn’t work at me?

Flags: X - disabled 
 #   NETWORK              SYNCHRONIZE
 0   ;;; test
     2.2.2.2/32           no



#!/usr/bin/php
<?php

//prikazi greske
//error_reporting( 0 );
//error_reporting( E_ALL );
//ini_set( 'display_errors', 'On' );
//define( "_VER", '1.0' );

$fecha_now = date("Y-m-d H:i:s", time());

/* KONEKCIJA NA MIKROTIK ROUTER */
$cfg[ ip_mikrotik ] = "1.1.1.1"; // IP adresa routera
$cfg[ api_user ]    = "user"; // User
$cfg[ api_pass ]    = "pass"; // Password

require_once "routeros_api.php";
$API = new RouterosAPI();
$API->debug = true;


$API->connect( $cfg[ ip_mikrotik ], $cfg[ api_user ], $cfg[ api_pass ] );

// Ispisi u logu routera
$comment_rule = 'GlobalHost Guard je restartan i BGP mreze su flushane ' .$fecha_now;
$API->write( '/log/info', false );
$API->write( '=message=' . $comment_rule );

// Pokreni skriptu na routeru


$API->write( '/routing/bgp/network/print', false );
$API->write('?comment=test', false);
$API->write('=.proplist=.id');
$ARRAYS = $API->read();

$API->write('/routing/bgp/network/remove', false);
$API->write('=.id=');
$READ = $API->read();
?>

..

<<< [6] /login
>>> [5/5] bytes read.
>>> [5, 39]!done
>>> [37/37] bytes read.
>>> [37, 1]=ret=27468875b4a69bd2b43de3ed9d281c76
<<< [6] /login
<<< [13] =name=fnmuser
<<< [44] =response=00d6e57c930239806eef52b0790f4101d0
>>> [5/5] bytes read.
>>> [5, 1]!done
Connected...
<<< [9] /log/info
<<< [82] =message=GlobalHost Guard je restartan i BGP mreze su flushane 2018-09-04 14:37:53
<<< [26] /routing/bgp/network/print
<<< [13] ?comment=test
<<< [14] =.proplist=.id
>>> [5/5] bytes read.
>>> [5, 1]!done
<<< [27] /routing/bgp/network/remove
<<< [5] =.id=
>>> [3/3] bytes read.
>>> [3, 17]!re
>>> [8/8] bytes read.
>>> [8, 8]=.id=*50
>>> [5/5] bytes read.
>>> [5, 1]!done
Disconnected...

Because you didn’t specify ID to remove.

  1. run print
  2. get ID
  3. use ID to remove network

Hm, this is not an option in my situation. If i need to enter ID in script, I can remove manual via winbox or telnet, don’t need PHP API script.
Is there solution to remove all networks with comment “test”?

It should be done automatically by your app. That is how API works and how it should be used.