Mikrotik API call not working

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

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?

Yes, make sure you are using new authentication method.

Same problem, i’m using same API class like you and all my systems died

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?

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!

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.

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

Not true. See how it is done in our api example client:
https://wiki.mikrotik.com/wiki/Manual:API#Example_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

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

We will add note regarding API, too.

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-netprovider/commit/8bc3bb2e163b11e5306e581e14f78073647463b3

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

Looking forward to the release! :smiley:

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

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 .

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