Page 1 of 1

Mikrotik API call not working

Posted: Fri Jun 28, 2019 5:34 pm
by amardeep704
Hi Guys,

I am using the Mikrotik API (php) to connect with the gateway and enable/disable hotspot. However, the API connect function is return false.

Sample code:

require('routeros_api.class.php');
$API = new RouterosAPI();

if ($API->connect('IP', 'USERNAME', 'PASSWORD')) {
die('connnect');
}

Any thoughts?

Thanks

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 9:58 am
by mrz
Make sure your PHP code is using new authentication method:
https://wiki.mikrotik.com/wiki/Manual:API#Initial_login

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 12:20 pm
by imort
Make sure your PHP code is using new authentication method:
https://wiki.mikrotik.com/wiki/Manual:API#Initial_login
Have same issue with upgrade to the new 6.45.1 firmware.
API authentication fails, and same is working with 6.44.3

Is it related with removed unsafe user passwords storage?

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 12:27 pm
by mrz
Yes, make sure you are using new authentication method.

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 12:41 pm
by Kurosudo
Same problem, i'm using same API class like you and all my systems died

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 12:44 pm
by imort
Yes, make sure you are using new authentication method.
I think that a proper warning about deprecated API authentication with this release was at least essential.
Problem is that I have a strange success/fail API authentication entries in the logs even after downgrade and restoring the last backup.

Can you please suggest should downgrade restore old-API authentication working, before third-party software can fix it from their side?

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 12:54 pm
by mrz
There is already important note due to removal of compatibility with old version passwords.
Third party software should have been fixed long time ago ,new authentication method was introduced back in May 2018.

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 1:00 pm
by imort
There is already important note due to removal of compatibility with old version passwords.
Third party software should have been fixed long time ago ,new authentication method was introduced back in May 2018.
Sorry, can't see anything about deprecation of old-style authentication both in the changelogs or API link you posted above.
Can you please post a link to the important note you're mention?

Thank you!

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 1:04 pm
by mrz
Important note!!!
Due to removal of compatibility with old version passwords in this version...
...
!) user - removed insecure password storage;
...

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 1:16 pm
by imort
Important note!!!
Due to removal of compatibility with old version passwords in this version...
...
!) user - removed insecure password storage;
...
I still can't see any API-related warnings in the release.
Just to be sure that you can understand my point:

1. Removing compatibility with old version passwords
Ok, I really can't say that it should be intuitive for any user that it will affect their API authentication somehow.

2. user - removed insecure password storage;
Same here.

I didn't trying to hype on that, but I strongly believe that another important notice about old-style API authentication deprecation starts with this release will be very pleased for many MikroTik users.

Thank you.

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 1:48 pm
by Kurosudo
It is possible to fix it via little dirty hack in API. Just change login sequence. But This will make API usable only 6.43 higher

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 2:02 pm
by mrz
Not true. See how it is done in our api example client:
https://wiki.mikrotik.com/wiki/Manual:A ... ple_client

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 2:15 pm
by Kurosudo
Not true. See how it is done in our api example client:
https://wiki.mikrotik.com/wiki/Manual:A ... ple_client
Yea, i basically did same thing in my patch i just wrote "how it fastly fix".
Infact i don't see any advantage to use old style API because having unpatched ROS (pre-6.43) is big security hole

Re: Mikrotik API call not working

Posted: Mon Jul 01, 2019 6:38 pm
by amardeep704
Hi Guys,

I am able to connect using the new authentication method. Sample code:

use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b6.phar';

try{
$util = new RouterOS\Util($client = new RouterOS\Client('IP', 'USERNAME', 'PASSWORD'));
foreach ($util->setMenu('/log')->getAll() as $entry) {
echo $entry('time') . ' ' . $entry('topics') . ' ' . $entry('message') . "\n";
}
} catch (Exception $e) {
echo 'Unable to connect to RouterOS.';
}

It lists the logs on the router.

I am trying to enable/disable the hotspot(p hotspot disable 0) but can't find the property that will be used to achieve this in the above code.

I am also trying to print the interface list but nothing ie being returned.

Sample code:

$getRequest = new RouterOS\Request('/interface/get');
$getRequest->setArgument('value-name', 'type');
$getRequest->setArgument('number', 'ether1');
$type = $client->sendSync($getRequest)->getProperty('ret');
print_r($type);

Any thoughts?

Thanks

Re: Mikrotik API call not working

Posted: Tue Jul 02, 2019 12:46 pm
by mrz
I didn't trying to hype on that, but I strongly believe that another important notice about old-style API authentication deprecation starts with this release will be very pleased for many MikroTik users.
Thank you.
We will add note regarding API, too.

Re: Mikrotik API call not working

Posted: Wed Jul 03, 2019 12:12 am
by stealth
For those who are using old good routeros_api.class.php from Denis Basta. The urls are no longer active. The needed changes to make it work again with new /login

https://github.com/Ftelf/netprovider-ne ... 73647463b3

Re: Mikrotik API call not working  [SOLVED]

Posted: Wed Jul 03, 2019 9:21 am
by boen_robot
Right now, PEAR2_Net_RouterOS supports the new login method only in the development version. That is, the currently unreleased 1.0.0b7. You can only get it via Composer by specifying "dev-develop" as your required version.

Generating PHARs is a little bit difficult and time consuming right now, which is why I haven't done it yet... Since these new versions no longer support the old authentication method, I'll try to make a release this weekend.

@amardeep704 I'm surprised b6 works to be honest. As for disabling hotspot, you can use f.e.
$util->setMenu('/ip hotspot')->disable('hostspot1');
If your hostpot server instance is named "hotspot1".

Re: Mikrotik API call not working

Posted: Fri Jul 05, 2019 8:35 pm
by BinaryCrash
Generating PHARs is a little bit difficult and time consuming right now, which is why I haven't done it yet... Since these new versions no longer support the old authentication method, I'll try to make a release this weekend.
Looking forward to the release! :D

Re: Mikrotik API call not working

Posted: Wed Jul 17, 2019 6:42 pm
by machack
For Login Using Php API, you need change in: routeros_api.classv1.6.php

$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));

for

$this->write('=password=' . $password);

Re: Mikrotik API call not working

Posted: Tue Aug 13, 2019 12:50 pm
by benkhlifahaythem
Not true. See how it is done in our api example client:
https://wiki.mikrotik.com/wiki/Manual:A ... ple_client

Good day,

i did apply the new login method and i still get the same issue with V6.45.1 and up im using php .

Code: Select all


<?php


use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b6.phar';

try{
$util = new RouterOS\Util($client = new RouterOS\Client('ip','user','password'));
foreach ($util->setMenu('/log')->getAll() as $entry) {
echo $entry('time') . ' ' . $entry('topics') . ' ' . $entry('message') . "\n";
}
} catch (Exception $e) {
die($e);
}

?>



-------------------------------------OUT PUT--------------------------------------------------------------


PEAR2\Net\RouterOS\DataFlowException: Invalid username or password supplied. in phar://C:/xampp/htdocs/test2/PEAR2_Net_RouterOS-1.0.0b6.phar/PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Net/RouterOS/Client.php:175 Stack trace: #0 C:\xampp\htdocs\test2\example.php(8): PEAR2\Net\RouterOS\Client->__construct('ip', 'user', 'password') #1 {main}



I Don't figure it out why I'm getting the error.

Re: Mikrotik API call not working

Posted: Tue Aug 13, 2019 3:35 pm
by Jotne
Fixed

Re: Mikrotik API call not working

Posted: Tue Aug 20, 2019 2:49 pm
by nrtszr
when will it come out?

PEAR2_Net_RouterOS-1.0.0b7.phar

thanks

Re: Mikrotik API call not working

Posted: Mon Aug 26, 2019 3:24 pm
by BinaryCrash
when will it come out?

PEAR2_Net_RouterOS-1.0.0b7.phar

thanks
I would like to know too.

Re: Mikrotik API call not working

Posted: Mon Sep 02, 2019 11:14 am
by mvalsasna
Hi

also looking forward to the release of Net_RouterOS-1.0.0b7

thanks

MAtteo
when will it come out?

PEAR2_Net_RouterOS-1.0.0b7.phar

thanks

Re: Mikrotik API call not working

Posted: Sat Sep 07, 2019 3:48 pm
by tayyebjaved
For Login Using Php API, you need change in: routeros_api.classv1.6.php

$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));

for

$this->write('=password=' . $password);
Not working for me.

Re: Mikrotik API call not working

Posted: Tue Nov 05, 2019 8:48 pm
by LeoCombes
Hi

also looking forward to the release of Net_RouterOS-1.0.0b7

thanks

MAtteo
when will it come out?

PEAR2_Net_RouterOS-1.0.0b7.phar

thanks
UP! :)

Re: Mikrotik API call not working

Posted: Tue Nov 05, 2019 9:17 pm
by BinaryCrash
Hi

also looking forward to the release of Net_RouterOS-1.0.0b7

thanks

MAtteo
when will it come out?

PEAR2_Net_RouterOS-1.0.0b7.phar

thanks
UP! :)
UP!

Re: Mikrotik API call not working

Posted: Mon Nov 11, 2019 10:53 pm
by gu1113
Hello, can anyone provide the download link for PEAR2_Net_RouterOS-1.0.0b7.phar ?

I cant see the update anywhere ... :-(

Re: Mikrotik API call not working

Posted: Mon Nov 18, 2019 4:23 pm
by amardeep704
Hello, can anyone provide the download link for PEAR2_Net_RouterOS-1.0.0b7.phar ?

I cant see the update anywhere ... :-(
You can check the link - https://github.com/pear2/Net_RouterOS/wiki/Util-basics

Re: Mikrotik API call not working

Posted: Tue Dec 03, 2019 8:23 pm
by LeoCombes
Hello, can anyone provide the download link for PEAR2_Net_RouterOS-1.0.0b7.phar ?

I cant see the update anywhere ... :-(
You can check the link - https://github.com/pear2/Net_RouterOS/wiki/Util-basics

I cant see how to download phar file from the page that you suggest •`_´•

Re: Mikrotik API call not working

Posted: Fri Dec 06, 2019 4:40 pm
by LeoCombes
I was waiting for Net_RouterOS-1.0.0b7.phar solution because I never used composer and I have not time to learn how use it, not interesed to touch and doing tests over a production server, etc.
boen_robot says that he will generate a .phar file when a new release was published, so, for those people that are waiting for PEAR2_Net_RouterOS-1.0.0b7.phar because not are a composer user and need a quick solution... here are which steps I followed in Linux:

- get another machine whith PHP installed (or install it) for generate the appropiate files, no need of touch a production server for this.
mkdir temp
cd temp
wget https://getcomposer.org/download/1.9.1/composer.phar
If you have a older PHP version (5.6 for example) maybe you will be interested in installing an older version of composer from https://getcomposer.org/download/.
php composer.phar require pear2/net_transmitter:1.0.0b1 pear2/cache_shm pear2/net_routeros:dev-develop@dev
If everything goes well, now you have a 'vendor' directory with some composer files. If you want verify that is the correct version, you can search for '6.43' in the file 'vendor/pear2/net_routeros/src/PEAR2/Net/RouterOS/Client.php' and find the new login method.
Move this 'temp' directory to your server, rename it to something meaningful, like 'composer', and include in your old script, with the correct path, for example:
replace
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';
with:
require __DIR__ . '/../composer/vendor/autoload.php';
This is all.

Re: Mikrotik API call not working

Posted: Thu Feb 06, 2020 7:21 pm
by electravis
I am having some issues with API login still. I have tried all that was mentioned in this form and nothing seems to be working. Has anyone had success and is able to step by step it? I updated the Basta AP.php file tried changing the variables and still nothing is working for me.

Here is the API part I think matters
public function connect($ip, $login, $password)
{
for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
$this->connected = false;
$PROTOCOL = ($this->ssl ? 'ssl://' : '' );
$context = stream_context_create(array('ssl' => array('ciphers' => 'ADH:ALL', 'verify_peer' => false, 'verify_peer_name' => false)));
$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $this->port . '...');
$this->socket = @stream_socket_client($PROTOCOL . $ip.':'. $this->port, $this->error_no, $this->error_str, $this->timeout, STREAM_CLIENT_CONNECT,$context);
if ($this->socket) {
socket_set_timeout($this->socket, $this->timeout);
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=password=' . $password);
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
$this->connected = true;
break;


Then here is my test code I am using

<?php

require('ros_new_api.php');

$API = new RouterosAPI();

$API->debug = true;

if ($API->connect('1.1.1.1', 'admin', 'password')) {

$API->write('/ip/hotspot/hotspot/getall');
$ARRAY = $API->read();

print_r($ARRAY);

$API->disconnect();

}

?>


ANy help.

Thank you

Re: Mikrotik API call not working

Posted: Thu Apr 02, 2020 7:44 am
by jmmr84
Hi everyone, for all of you that are workin with PEAR2 API, here is the solution:

1. Uncompress the .phar file
2. Go to the uncompressed folder -> PEAR2_Net_RouterOS-1.0.0b6\src\PEAR2\Net\RouterOS and edit the Client.php file
3. Find the _login function and modify it to look exactly like this:

private static function _login(
Communicator $com,
$username,
$password = '',
$timeout = null
) {
$request = new Request('/login');
$request->setArgument('name', $username);
$request->setArgument('password', $password);
$request->verify($com)->send($com);

$response = new Response($com, false, $timeout);
if ($response->getType() === Response::TYPE_FINAL) {
return null === $response->getProperty('ret');
} else {
while ($response->getType() !== Response::TYPE_FINAL
&& $response->getType() !== Response::TYPE_FATAL
) {
$response = new Response($com, false, $timeout);
}
return false;
}
}


4. Finally make sure you have the uncompressed and edited folder where you had your .phar file and change your project's php like this:

Replace -> require_once 'PEAR2_Net_RouterOS-1.0.0b6.phar'; for -> require_once 'PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Autoload.php';

Now you should be able to connect with the new versions of RouterOS.

Re: Mikrotik API call not working

Posted: Tue Apr 14, 2020 2:33 am
by afamative
Not true. See how it is done in our api example client:
https://wiki.mikrotik.com/wiki/Manual:A ... ple_client

Good day,

i did apply the new login method and i still get the same issue with V6.45.1 and up im using php .

Code: Select all


<?php


use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b6.phar';

try{
$util = new RouterOS\Util($client = new RouterOS\Client('ip','user','password'));
foreach ($util->setMenu('/log')->getAll() as $entry) {
echo $entry('time') . ' ' . $entry('topics') . ' ' . $entry('message') . "\n";
}
} catch (Exception $e) {
die($e);
}

?>



-------------------------------------OUT PUT--------------------------------------------------------------


PEAR2\Net\RouterOS\DataFlowException: Invalid username or password supplied. in phar://C:/xampp/htdocs/test2/PEAR2_Net_RouterOS-1.0.0b6.phar/PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Net/RouterOS/Client.php:175 Stack trace: #0 C:\xampp\htdocs\test2\example.php(8): PEAR2\Net\RouterOS\Client->__construct('ip', 'user', 'password') #1 {main}



I Don't figure it out why I'm getting the error.
where you able to fix this?

Re: Mikrotik API call not working

Posted: Sun Apr 19, 2020 4:39 pm
by christophersarnillo
Hi everyone, for all of you that are workin with PEAR2 API, here is the solution:

1. Uncompress the .phar file
2. Go to the uncompressed folder -> PEAR2_Net_RouterOS-1.0.0b6\src\PEAR2\Net\RouterOS and edit the Client.php file
3. Find the _login function and modify it to look exactly like this:

private static function _login(
Communicator $com,
$username,
$password = '',
$timeout = null
) {
$request = new Request('/login');
$request->setArgument('name', $username);
$request->setArgument('password', $password);
$request->verify($com)->send($com);

$response = new Response($com, false, $timeout);
if ($response->getType() === Response::TYPE_FINAL) {
return null === $response->getProperty('ret');
} else {
while ($response->getType() !== Response::TYPE_FINAL
&& $response->getType() !== Response::TYPE_FATAL
) {
$response = new Response($com, false, $timeout);
}
return false;
}
}


4. Finally make sure you have the uncompressed and edited folder where you had your .phar file and change your project's php like this:

Replace -> require_once 'PEAR2_Net_RouterOS-1.0.0b6.phar'; for -> require_once 'PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Autoload.php';

Now you should be able to connect with the new versions of RouterOS.

Re: Mikrotik API call not working

Posted: Thu Jun 18, 2020 11:47 pm
by szwedolecko
For Login Using Php API, you need change in: routeros_api.classv1.6.php

$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));

for

$this->write('=password=' . $password);
Not working for me.
its worked for me ;) thanks

Re: Mikrotik API call not working

Posted: Sun Sep 27, 2020 11:01 pm
by salman4u
When api is not connecting to the router and showing disconnect....., you need to change in: routeros_api.class.php
search (Login to RouterOS) in routeros_api.class.php
add this code
comment this line showing in code and add this code above this :
$this->write('=password=' . $password);
//$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));

function connect($ip, $login, $password)
{
for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
$this->connected = false;
$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $ip . ':' . $this->port . '...');
$this->socket = @fsockopen($ip, $this->port, $this->error_no, $this->error_str, $this->timeout);
if ($this->socket) {
socket_set_timeout($this->socket, $this->timeout);
$this->write('/login');
$RESPONSE = $this->read(false);
if ($RESPONSE[0] == '!done') {
$MATCHES = array();
if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
$this->write('/login', false);
$this->write('=name=' . $login, false);

..................................////////..........add this code....................
$this->write('=password=' . $password);
................................///////////.........comment this code.......................and save it
//$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));


$RESPONSE = $this->read(false);
if ($RESPONSE[0] == '!done') {
$this->connected = true;
break;
}
}
}
}
fclose($this->socket);
}
sleep($this->delay);
}
if ($this->connected){
$this->debug('Connected...');
}else{
$this->debug('Error...');
}
return $this->connected;
}


/**
* Disconnect from RouterOS
*
* @return void
*/
function disconnect()
{
fclose($this->socket);
$this->connected = false;
$this->debug('Disconnected...');
}
comment this code
$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));

for

$this->write('=password=' . $password);

Re: Mikrotik API call not working

Posted: Sat Aug 05, 2023 12:36 pm
by Yako2023
For Login Using Php API, you need change in: routeros_api.classv1.6.php

$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));

for

$this->write('=password=' . $password);
Thanks man you really help me !!!! I waste a cup of days trying this f... error, thank you very much!
hasta la vista