Community discussions

MikroTik App
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Remove BGP network by comment via PHP API

Tue Sep 04, 2018 11:14 am

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"]' );

2.
$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!
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 11:17 am

comment~test is not a valid query.

See the manual for valid queries:
https://wiki.mikrotik.com/wiki/Manual:API#Queries
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 11:25 am

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.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 11:28 am

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.
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 11:37 am

Ok, i will try to add to router script "routing bgp network remove [find comment~"Fast"]" and run this script via PHP API.
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 11:54 am

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?
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 12:11 pm

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?
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 1:27 pm

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"
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 1:41 pm

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();
?>
Last edited by ropeba on Tue Sep 04, 2018 2:49 pm, edited 1 time in total.
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 2:49 pm

Maybe a timing issue? Are you sure that the output of $API->read(false); is "!done"?
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 2:56 pm

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 :)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 3:02 pm

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.
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 3:06 pm

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();
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 3:14 pm

You haven't specified any ID to remove.
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 3:21 pm

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');
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 3:33 pm

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
>>> 

 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 3:41 pm

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...
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 3:58 pm

Because you didn't specify ID to remove.

1) run print
2) get ID
3) use ID to remove network
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 4:09 pm

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"?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 4:11 pm

It should be done automatically by your app. That is how API works and how it should be used.
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 4:14 pm

It should be done automatically by your app. That is how API works and how it should be used.
Maybe we misunderstand. You say that I need to enter ID by login in Mikrotik console manual, enter /ip rou bgp pri and copy ID manual to PHP script, or think that I need to get ID through PHP script?
I hope that you mean on getting ID via PHP code. If yes, please can you share your code, which work ok?
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 5:24 pm

Working PHP example:

<?php

require_once "routeros_api.class.php"; // https://github.com/BenMenking/routeros-api

$API = new RouterosAPI();
$API->debug = true;

if ($API->connect('192.168.88.1', 'user', 'pass'))
{
  $networks = $API->comm('/routing/bgp/network/print', array(
    '?comment' => 'test',
    '.proplist' => '.id'
  ));

  foreach ($networks as $network)
  {
    $API->comm('/routing/bgp/network/remove', array('.id' => $network['.id']));
  }

  $API->disconnect();
}

?>


Execution:

Connection attempt #1 to 192.168.88.1:8728...
<<< [6] /login
>>> [5/5] bytes read.
>>> [5, 39]!done
>>> [37/37] bytes read.
>>> [37, 1]=ret=24625dc395367aef89e628557f7bd582
<<< [6] /login
<<< [10] =name=user
<<< [44] =response=00ec760df10f86869e2f174de3aef7b450
>>> [5/5] bytes read.
>>> [5, 1]!done
Connected...
<<< [26] /routing/bgp/network/print
<<< [13] ?comment=test
<<< [14] =.proplist=.id
>>> [3/3] bytes read.
>>> [3, 16]!re
>>> [7/7] bytes read.
>>> [7, 8]=.id=*E
>>> [5/5] bytes read.
>>> [5, 1]!done
<<< [27] /routing/bgp/network/remove
<<< [7] =.id=*E
>>> [5/5] bytes read.
>>> [5, 1]!done
Disconnected...
Disconnected...

Before:

/routing bgp network
add network=3.3.3.3/32 synchronize=no
add comment=test network=2.2.2.2/32 synchronize=no

After:

/routing bgp network
add network=3.3.3.3/32 synchronize=no
 
ropeba
Member Candidate
Member Candidate
Topic Author
Posts: 220
Joined: Sat Jul 29, 2006 4:13 pm

Re: Remove BGP network by comment via PHP API

Tue Sep 04, 2018 7:12 pm

That's I need :) thank you so much nescafe2002!

Who is online

Users browsing this forum: Bing [Bot] and 13 guests