Community discussions

MikroTik App
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

PHP APi Connection

Fri Oct 16, 2020 1:49 pm

Hi Guys,

I am using the Mikrotik API PHP (RouterOS PHP API class v1.6) to connect and read the queue types & its Rates. However, the API connect function is return false.
But works fine with WINBOX using the same login credentials.
I can able to see the connection attempt as 'login failure for user xxxx from xxx.xxx.xxx.xx via api' at WINBOX logs.
Enabled api with port on 'IP services list'.

Sample code:

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

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

When i run the code, it says 'invalid user name or password - Unable to connect to RouterOS ' . But the same login credentials works fine with WINBOX

Any thoughts?

Thanks
You do not have the required permissions to view the files attached to this post.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6130
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: PHP APi Connection

Fri Oct 16, 2020 2:00 pm

https://wiki.mikrotik.com/wiki/Manual:API#Initial_login

see notes in login process post-v6.43
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

Re: PHP APi Connection

Fri Oct 16, 2020 2:29 pm

This is my connection function code from routeros_api.class.php

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


How can i modify it to Work?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6130
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: PHP APi Connection

Fri Oct 16, 2020 2:31 pm

as described in the link I provided.

send /login =name= =password= in first message
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

Re: PHP APi Connection

Fri Oct 16, 2020 2:37 pm

Thanks for your guidance.
Modified the code like this. Still same error. Did I miss something?

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');
$this->write('=name=' . $login, false);
$this->write('=password=' . $password, false);
$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);
$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;
}
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6130
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: PHP APi Connection

Fri Oct 16, 2020 2:54 pm

there is no need to send /login again with md5 hash
 
Sob
Forum Guru
Forum Guru
Posts: 6096
Joined: Mon Apr 20, 2009 9:11 pm

Re: PHP APi Connection

Fri Oct 16, 2020 3:19 pm

People who quote full posts should be spanked with ethernet cable. Some exceptions for multi-topic threads may apply. Not intended as incentive for masochists.
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

Re: PHP APi Connection

Fri Oct 16, 2020 3:20 pm

Thank you.. but Still the same error. $RESPONSE = $this->read(false) returns just an empty array. Can you please the code now

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');
$this->write('=name=' . $login, false);
$this->write('=password=' . $password, false);
$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) {
$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;
}
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

Re: PHP APi Connection

Fri Oct 16, 2020 3:24 pm

Thanks for the class file. But still the same issue.. I think the prob is not from class file. Look like something else
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

Re: PHP APi Connection

Fri Oct 16, 2020 3:46 pm

Issue maybe from any ip blocking or any firewall settings? Why my connection is refusing by server?
Any ideas?
 
Sob
Forum Guru
Forum Guru
Posts: 6096
Joined: Mon Apr 20, 2009 9:11 pm

Re: PHP APi Connection

Fri Oct 16, 2020 3:50 pm

If you can see connection attempt in router's log, then it clearly passed through firewall, otherwise it wouldn't be there at all.
People who quote full posts should be spanked with ethernet cable. Some exceptions for multi-topic threads may apply. Not intended as incentive for masochists.
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

Re: PHP APi Connection

Fri Oct 16, 2020 4:07 pm

Yes you are right! I don't know whats the exact issue.. Can you please help me to check it?
 
Sob
Forum Guru
Forum Guru
Posts: 6096
Joined: Mon Apr 20, 2009 9:11 pm

Re: PHP APi Connection

Fri Oct 16, 2020 4:25 pm

The class I linked to works fine for me, so I don't know what can be wrong. Add some more debug output in connect(), just simple "echo" is fine, to see what exactly fails. Dumping $RESPONSE is good start.
People who quote full posts should be spanked with ethernet cable. Some exceptions for multi-topic threads may apply. Not intended as incentive for masochists.
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

Re: PHP APi Connection

Fri Oct 16, 2020 4:32 pm

Ok sure.
Result of : $RESPONSE = $this->read(false) :

Array
(
[0] => !trap
[1] => =message=std failure: not allowed (9)
[2] => !done
)
 
Sob
Forum Guru
Forum Guru
Posts: 6096
Joined: Mon Apr 20, 2009 9:11 pm

Re: PHP APi Connection

Fri Oct 16, 2020 5:06 pm

I don't know what that is, but maybe some problem with permissions for used username?
People who quote full posts should be spanked with ethernet cable. Some exceptions for multi-topic threads may apply. Not intended as incentive for masochists.
 
karthikpenta
just joined
Topic Author
Posts: 10
Joined: Fri Oct 16, 2020 1:13 pm

Re: PHP APi Connection

Sat Oct 17, 2020 7:13 am

Ohh ok.. I will check and get back to you.

Who is online

Users browsing this forum: Google [Bot] and 125 guests