Community discussions

MikroTik App
 
edder
just joined
Topic Author
Posts: 3
Joined: Wed Feb 21, 2018 12:48 am

Help me please, I'm going crazy whit the API!

Wed Feb 21, 2018 1:01 am

Hello, my problem is as follows, I am trying to make a web application using the Mikrotik API which must by clicking on a button on the page renew the IP of the interface that I sent, I have managed to connect to the team from the API since I open the window of the logs and it shows me that it is logging well, but when I try to send the command it does not do anything. I am putting the command in the following way.

$API = new routeros_api();
if($API->connet(1.1.1.1, admin, 1234, 8728)){
$API->write('/ip/dhcp-client/release/1');
}
else
{
$API->disconnect();
}

my question is exactly in the form in which I should send the command, if I do it with write or with comm and the correct way to write it.
 
User avatar
ochaconm
Trainer
Trainer
Posts: 28
Joined: Fri Feb 22, 2013 9:39 pm

Re: Help me please, I'm going crazy whit the API!

Thu Feb 22, 2018 4:43 am

If you haven't found the solution yet, maybe this could help you.

You have to note that the item IDs on the API do not work exactly in the same way as in the console.
For a better explanation see: https://wiki.mikrotik.com/wiki/API_comm ... attributes

Try also dumping (var_dump($variable)) the results to see the array structure of the results from Mikrotik API.
$API = new routeros_api();
if($API->connet(1.1.1.1, admin, 1234, 8728)){
	
	//If you need something specific, you will have to make some modifications

	//Find all DHCP clients
	$arrResult = $API->comm ( '/ip/dhcp-client/print', array (
	"?criteria1" => 'criteria_value1',
	"?criteria2" => 'criteria_value2',
	) );
	
	
	//Release all dhcp-clients
	foreach ( $arrResult as $item ) {
		$API->write ( /ip/dhcp-client/release, false );
		$API->write ( '=numbers=' . $item ['.id'] );
		$API->read ( false );
	}
}
else
{
	$API->disconnect();
}
 
edder
just joined
Topic Author
Posts: 3
Joined: Wed Feb 21, 2018 12:48 am

Re: Help me please, I'm going crazy whit the API!

Thu Feb 22, 2018 6:34 am

Hello Mr. Ochaconm, thank you very much for your prompt response, I appreciate your help, but I still have some doubts to clarify, in the example that put me what should I put in the "? criteria1" => 'criteria_value1'? I need is simple, in my team I have several virtual interfaces configured and I need to be able to make a release to a specific interface, how do you think that would be the correct way to do it?

this would be an example of my code

Code: Select all

<?php require_once('api_mt_include2.php'); ?>
<?php

$ipRouteros="192.168.17.1";
$Username="reinicio";
$Pass="12345";
$api_puerto=8728;
$name="wlan2"; //

$API = new routeros_api();
$API->debug = true;
if ($API->connect($ipRouteros , $Username , $Pass, $api_puerto)){
$API->write ('/ip/dhcp-client/release', false);
$API->write ( '=.id=',3);
$API->read ( false );
}
else
{
$API->disconnect();
}
I have a question specifically in this line $ API-> write ('= .id =', 3) ;, how do I specify that I want to release the wlan3 interface?

Thank you very much once more

this is the answer of the debug
Connection attempt #1 to 192.168.17.1:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 39]!done >>> [37/37] bytes read. >>> [37, 1]=ret=5c4f9601961333c7ae3c52186747afad <<< [6] /login <<< [14] =name=reinicio <<< [44] =response=00dd17f4768dabf754094fa7889e71a573 >>> [5/5] bytes read. >>> [5, 1]!done Connected... <<< [23] /ip/dhcp-client/release <<< [5] =.id= <<< [6] .tag=3 >>> [5/5] bytes read. >>> [5, 8]!done >>> [6/6] bytes read. >>> [6, 1].tag=3
 
User avatar
ochaconm
Trainer
Trainer
Posts: 28
Joined: Fri Feb 22, 2013 9:39 pm

Re: Help me please, I'm going crazy whit the API!

Fri Feb 23, 2018 12:35 am

In the line
$ API-> write ('= .id =', 3)
, you should not send the parameter "3", insted of that you have to first get the internal ".id" (also using the API).

A way to get the ".id", maybe not the optimal, is as follows:
//Get the DHCP-CLIENT details that match with interface wlan1
$arrResult = $API->comm ( '/ip/dhcp-client/print', array ("?interface" => 'wlan1') );
Play around with the previous line of code, e.g you could send no parameters to get all DHCP-clients or search by your custom criteria.
Note that the arrResult variable will contain all the DHCP-clients that matched with the query. You could use this array to build a previous screen where the user can select the desired interface, this is up to you.
//Get DHCP-CLIENT details of all (empty array).
$arrResult = $API->comm ( '/ip/dhcp-client/print', array () );

I tested the following code and it releases the dhcp-client set on wlan1:
<?php
require('routeros_api.class.php');
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('192.168.88.1', 'admin', '')) {

	//Find DHCP clients on wlan1
	$arrResult = $API->comm ( '/ip/dhcp-client/print', array ("?interface" => 'wlan1') );
	
	var_dump($arrResult);//Just for debugging purposes
	
	//Release all dhcp-clients
	foreach ( $arrResult as $item ) {
		$API->write ( '/ip/dhcp-client/release', false );
		$API->write ( '=numbers=' . $item ['.id'] );
		$API->read ( false );
	}
}
else
{
	$API->disconnect();
}
I hope this clarifies your doubts
 
User avatar
ochaconm
Trainer
Trainer
Posts: 28
Joined: Fri Feb 22, 2013 9:39 pm

Re: Help me please, I'm going crazy whit the API!

Fri Feb 23, 2018 12:40 am

I forget to mention that I'm using the API connection class from:

https://github.com/BenMenking/routeros- ... .class.php

You can also get some examples in this (BenMenking) GitHub.

It appears that you are using the same library, but just in case of some problem.

Greetings.
 
edder
just joined
Topic Author
Posts: 3
Joined: Wed Feb 21, 2018 12:48 am

Re: Help me please, I'm going crazy whit the API!

Fri Feb 23, 2018 1:04 am

Hello Mr. Ochaconm, thank you very much for your help, without you I think I would have gone crazy, thanks to your examples I managed to solve my problem and everything is working as I wanted.

Who is online

Users browsing this forum: No registered users and 24 guests