basic help with PHP API

Hello,

I`m trying PHP API with test router - v3.23 API service enabled.

I wanna add test user on test router, but without success.

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

if ($API->connect('10.11.10.11', 'user', 'pass')) {

		$API->write('/user add group=full name=test22 password=test123', false);
		$API->disconnect();

here is the output from the browser:

Connection attempt #1 to 10.11.10.11:8728… <<< [6] /login 5 >>> [5/5 bytes read. >>> [5, 39] !done 37 >>> [37/37 bytes read. >>> [37, 1] =ret=17d81181c9f0de35c61bbceb7c15bb0d 0 <<< [6] /login <<< [15] =name=user <<< [44] =response=0079ca715e7e6ffd9c37ecddbafbc8d425 5 >>> [5/5 bytes read. >>> [5, 1] !done 0 Connected… <<< [48] user add group=full name=test22 password=test123 Disconnected…

log on test router shows logged in logged out, but no user added, I have tried some other CLI command but without success.

Thank you for any kind of help in advance.
S.

You can’t send exact console command via api. API has it’s own syntax
http://wiki.mikrotik.com/wiki/API#Protocol

In PHP api it will look like this:

$API->write('/user/add', false);
$API->write('=group=full', false);
$API->write('=name=test22', false);
$API->write('=password=test123');

Thank you mrz for your reply,

I got the point, however with your code I didn`t got new user on my test router, browser output is:

Connection attempt #1 to 10.11.10.11:8728… <<< [6] /login 5 >>> [5/5 bytes read. >>> [5, 39] !done 37 >>> [37/37 bytes read. >>> [37, 1] =ret=0d12ec731390d33d96d7596616b9b3bb 0 <<< [6] /login <<< [15] =name=user<<< [44] =response=001362a8e999dd714eea38c55281adaa67 5 >>> [5/5 bytes read. >>> [5, 1] !done 0 Connected… <<< [9] /user/add <<< [10] group=full <<< [11] name=test22 <<< [16] password=test123 Disconnected…

I`ll read more about API syntax but seems that using SSH for PHP would be better solution since I can pass variables and use regular CLI syntax as I use in Terminal window…

look at my post above again, parameters were missing one symbol :slight_smile:

well, still nothing :frowning:… anyway now I`m keen to try solution were I can be more comfortable - execute regular CLI command, get output and parse it in web page.

I have installed php ssh2 (a bit tricky indeed) and works fine when I wanna connect to some other server and execute some shell command.

Here is what mikrotik says when I try to establish ssh connection with my test router:

Warning: ssh2_connect() [function.ssh2-connect]: Error starting up SSH connection(-5): Unable to exchange encryption keys in /home/xxx/public_html/xxx/test/ssh2.php on line 4

Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 10.11.126.250 in /home/xxx/public_html/xxx/test/ssh2.php on line 4
fail: unable to establish connection

Any help about establishing ssh connection with mikrotik?

Best regards,
S.

it works finally!

topic: http://forum.mikrotik.com/t/php-ssh-script-that-works-in-v2-9-fails-on-v3/20009/1 helped me to setup connection:

$methods = array ( ‘kex’ => ‘diffie-hellman-group1-sha1’ );
if(!($con = ssh2_connect(“10.11.10.11”, 22, $methods))){
echo “fail: unable to establish connection\n”;

. . . .

and now I can run regular CLI command as I wanted.

I’m very interested! I would love to find out more inforamtion related to this topic. Thanks in advance.
me too, I need more detailed info

[u]comparatif simulation taux pret auto[/u] - taux pret auto differe selon la prise en compte … calculent automatiquement le taux pour un prêt automobile donne.[u]comparatif simulation taux pret auto[/u]

Hello again,

all commands which I wanted to execute - manage users, playing around with firewall rules - have completed without any issues, syntax is exactly same as I wanted to do in CLI (what was the idea), but now I have the problem to get the OUTPUT?

I read many forums and there are lot of reports about it. However I`m able to get the output from some other Linux (testing “ls -al”) but without success with mikrotik?

here is the code:

            // collect returning data from command original
            stream_set_blocking( $stream, true );
            $data = "";
            while( $buf = fread($stream,4096) ){
                $data .= $buf;
            }
            fclose($stream);

I have tried also withoud while loop, but no still no success. I`m just waiting to resolve this and then I can fully integrate various scripts with our billing database :slight_smile:

Just one note, I`m able to get data from the test router using API, but I preferred to stay on this method since it would be really easy to integrate in existing system.

Best regards,
S.

this topic is about PHP API implementation - please create new topic for SSH question…

I had the same problem but with the command set.

I think the PHP API is not working completely and would be great find more examples in the manual.

You see the same problems but with the command set in another post:
http://forum.mikrotik.com/t/change-hotspot-password-with-php-api/34773/35

the same problem = the same solution!

But here the solution is not with API PHP. It work with SSH and shell. Is another solution for the same problem.

The solution for add or set user with API PHP is not appear. Or i´m wrong?

well, it’s here :wink: http://forum.mikrotik.com/t/basic-help-with-php-api/30578/2

Sorry. I know I am Brazilian and my English is not very good. But what is ninet says (i think) that the code did not work as I mentioned in another post. Soon we may conclude that the solution is not here.


ninet Response:

Thank you mrz for your reply,

I got the point, however with your > code I didn`t got new user > on my test router, browser output is:

Connection attempt #1 to 10.11.10.11:8728… <<< [6] /login 5 >>> [5/5 bytes read. >>> [5, 39] !done 37 >>> [37/37 bytes read. >>> [37, 1] =ret=0d12ec731390d33d96d7596616b9b3bb 0 <<< [6] /login <<< [15] =name=user<<< [44] =response=001362a8e999dd714eea38c55281adaa67 5 >>> [5/5 bytes read. >>> [5, 1] !done 0 Connected… > <<< [9] /user/add <<< [10] group=full <<< [11] name=test22 <<< [16] password=test123 > Disconnected…

The solution they found was with SSH2. Why you asked them to change the discussion to another site more appropriate since we’re talking about the PHP API.

I think I understand correctly, no?

Chupaka´s solution is here: http://forum.mikrotik.com/t/change-hotspot-password-with-php-api/34773/47

The code is working! Visit the link. Thx!

Hello Chupaka,

I´m here again, an. It´s good, no?

I hope you’re well. I prayed for you and your work …

Well, let´s go again:

I asked in the link below about limit time for password when i create user.

For example, after two hours the password lost and user need new access password.

It´s possible? Anybody know the command?

http://forum.mikrotik.com/t/how-to-remove-user-after-30-days-automaticlly/39957/4

automatically - no. you may set new password for user periodically, but you cannot logout connected user

Hello again Chupak,

I found a solution (i think). Look:

$tarefa = 'testetask';
$dt = 'Aug/26/2010';
$hr = '18:09:00';
$hrint = '00:01:00';
$politica = 'read,write,policy';
$comando = '/user remove teste; /system scheduler remove '.$tarefa;
							
$API->write('/system/scheduler/add', false);
$API->write('=name='.$tarefa, false);
$API->write('=start-date='.$dt, false);
$API->write('=start-time='.$hr, false);
$API->write('=interval='.$hrint, false);
$API->write('=policy='.$politica, false);
$API->write('=on-event='.$comando);

			
$ARRAY = $API->read();
							
$API->disconnect();

For someone who does not know how much this is a great discovery. If someone else is in the same situation as me, I think it will help.

A big hug to the community.

Without charity there is no salvation.

yep, something like this =)

unfortunately, if user is logged in and you remove the user, it’s not logged out automatically…

Chupaka,

Do you know anything about it:

http://forum.mikrotik.com/t/random-failure-in-connection-with-mikrotik-using-the-php-api/40497/1

It´s on line. Please one idea, one light.

Thx.