Problems with Centos 6 server and API Mikrotik

Hello, gentlemen …
Forgive me for bad English, but I am Brazilian and I am using google translator.

Recently set up a server Centos 6 following the following tutorial

http://www.howtoforge.com/perfect-server-centos-6.2-x86_64-with-apache2-ispconfig-3

This server will host a management system to access the Internet written in PHP / mysql and Mikrotik API to lock / unlock customers.

The whole routine PHP / API was working smoothly using wampp as a development platform (windows), but to put the files in PHP hosting (my server php / mysql centos 6), the functions of adding customers and managing permissions via API not responding. I see through the activity of Winbox login / logoff in RouterOS API commands as though Comm and write do not work.

Someone tell me if you need to do some kind of specific configuration commands for the API to work properly as well as worked in wampp?

Inform the Mikrotik is with the permission of API access enabled and all scripts php / mysql are working properly.

Thanks for help !

A big hug to everyone.

Does PHP produce any errors? Have you set your error level to “E_ALL | E_STRICT” (in both environments)? Maybe what was just a notice in WAMPP became important on CentOS.

It’s possible that your server firewall is not configured to allow outgoing connections to the router (though PHP should’ve complained if that’s the case, hence making sure you enable error reporting). Try disabling it temporarily to verify and if that’s the case, add the “php”, “php-cgi” and/or “apache” executables to the list of applications allowed to estabilish outgoing connections.

Friend Boen-robot

Your suggestion about CGI reminded me that the server configuration tutorial asks us to uncomment the line (; cgi.fix_pathinfo = 1) in php.ini. I returned the file and commented on it again and the scripts work, but now the values ​sent on variables are not included in RouterOS.

For example:

This works:

$ API-> comm ('/queue/simple/add', array (
"name" => "boenrobot"
));

This NOT works:

$ clientname = "boenrobot";

$ API-> comm ('/queue/simple/add', array (
"name" => $ clientname
));

Can you help me with this problem?

Variable names can’t have spaces. Turn

$ clientname

into

$clientname

(same for the “$ API” part)

Sure,

The variables name havn´t spaces… the spaces in names were created by google translator. I write my messages in notepad and copy/paste on google translator page…

I realized that even writing the values ​​directly in the array, not all the time that the values ​​are dropped in Routeros. if I refresh the page several times, the script includes the values ​​at will. I do not understand why …

Example…

refresh1 → Done
refresh2 → Error
refresh3 → Error
refresh4 → Error
refresh5 → Error
refresh6 → Done
refresh7 → Error
refresh8 → Error
refresh9 → Done
refresh10 → Error

Understood ??
I’m going crazy with this problem…

Thanks 4 all !!
Brazilian Hugs !

what error you get in reply? usually !re is quite descriptive

This is the “answer” by debug:

Connection attempt #1 to 192.168.55.1:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 0]!done Connection attempt #2 to 192.168.55.1:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 0]!done Connection attempt #3 to 192.168.55.1:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 0]!done Connection attempt #4 to 192.168.55.1:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 0]!done Connection attempt #5 to 192.168.55.1:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 0]!done Error... 

An error ocurred [if !done myself message]

After this I try again and debug returs:

Connection attempt #1 to 192.168.55.1:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 0]!done Connection attempt #2 to 192.168.55.1:8728... <<< [6] /login >>> [5/5] bytes read. >>> [5, 1]!done >>> [37/37] bytes read. >>> [37, 1]=ret=333d39b82d8e978ba92bc4f080f08fc7 <<< [6] /login <<< [9] =name=sge <<< [44] =response=00426ccaf36f522ae6422dd4dfb7a34c87 >>> [5/5] bytes read. >>> [5, 1]!done Connected... <<< [17] /queue/simple/add <<< [12] =name=teste5 <<< [32] =target-addresses=192.168.55.213 >>> [5/5] bytes read. >>> [5, 10]!done >>> [8/8] bytes read. >>> [8, 1]=ret=*17 

Done ! [if !done myself message]

thereafter I try again other times changing name/addresses values and didn´t work.

Very strange. It gives me impression that it only works when it wants.

My code:

<?php
require('mk/routeros_api.class.php');

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

if ($API->connect('192.168.55.1', 'sge', 'sge')) {      // Change this as necessery

$API->comm('/queue/simple/add', array(
  		"name" => "clientname",
		"target-addresses" => "clienteip"
		));

	echo "<br/>Done !";

} else {
	echo "<br/><br/>An error occurred";
}
echo "<br/><br/>";


?>

NOTE: I´m using a RB 750GL as testing Router. Her MK version is 5.11. I believe the version of the router that is serving the network is more current.

Do you think that the version influence the functioning of the API?

Looking at the log, it appears you’re having some issues with the connection between the server and the router.

The reason could still be in a misconfigured firewall, but if your router is many switches away and/or serving a good number of clients, a more likely cause is that your links are weak (a buggy switch/cable) and/or that the router is simply being overloaded.

Also, as a side note, check in the “Active Users” session that you don’t have any hanging connections (you shouldn’t have such with 5.*, but just in case…).

Thanks again…

Look, my connections are:

Internet link → RB750GL → Switcher => [server centos and my development machine]

This structure is only for developing, after everything is working perfectly, the server Centos will be connected to Mikrotik serving the entire network. ie, the only currently active connections in RB750GL are my development machine and the server Centos.

I return to say that the Centos firewall is disabled.

Excellent.

The only possible problem left is hanged connections. In the router, check the active users in “/user active”. You should only see one or two, and no “api” sessions. If there are more, it means some connections are left hanging for a while. If that’s the case, it might be wise to close them explicitly once you’re done by sending a “/quit” command and calling the disconnect() function.

Dude…
In /user/active have only my conection using winbox…

Would if I connect the Centos server directly to a port of RB it work?

If even that doesn’t work, I’m officially out of ideas.

Sure… I’ll try and come back with the result :smiley:

Unfortunately, it did not work. Anyway, I need a solution only for add/remove clients in the ARP table and /queue/simple, and lock / unlock customers in the queue. Do you indicate some other feature?

You mentioned in the start that it works with Windows… does it work at the very same node? That is, if you plug the windows machine in place of the CentOS (have the Windows machine directly connected to the router board), does it work?

If it does, this might be a PHP bug related to the way it uses sockets on CentOS. Solution: Use another kind of Linux and/or report this to the PHP/CentOS devs.

If even then it doesn’t work, this might be a bug in RouterOS that occurs only on that router board. Solution: Upgrade to the latest RouterOS, and if even then it doesn’t work, contact support.

Sure dude…

If I use my scripts in Wampp server on windows, the interaction between PHP/Mikrotik works great. My PHP version, installed on Centos is 5.3.3. The version of Routeros is 5.11 running on RouterBoard 750GL.

The centos I´m using is version 6.

Is Windows using the same PHP version? Again, this is a bug in either PHP or CentOS. So, if the version on CentOS is older than the one on Windows, it might be the cause. Try with PHP 5.3.10.

In Wampp the php version is 5.3.5 …

Then upgrade at least to that (ideally, upgrade to PHP 5.3.10 on both machines).

Looking at the changelogs for PHP 5.3.4, there have been a few stream related bug fixes… it’s possible they also affected sockets. In particular, the part

. Fixed forward stream seeking emulation in streams that don’t support seeking
in situations where the read operation gives back less data than requested
and when there was data in the buffer before the emulation started. Also
made more consistent its behavior – should return failure every time less
data than was requested was skipped. (Gustavo)

is probably the root of this here.

I think I am had and figured this problem out

I got the same behaviour/problem as the OP describes.

Its actually the php api class script is too fast for the hash.

To solve it edit the script and add:

sleep (2);

on a new line after line 85

Cheers.