Community discussions

MikroTik App
 
ninet
just joined
Topic Author
Posts: 14
Joined: Mon Aug 10, 2009 3:58 pm

basic help with PHP API

Mon Aug 10, 2009 4:07 pm

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.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7056
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: basic help with PHP API

Mon Aug 10, 2009 4:15 pm

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');
Last edited by mrz on Mon Aug 10, 2009 5:22 pm, edited 1 time in total.
 
ninet
just joined
Topic Author
Posts: 14
Joined: Mon Aug 10, 2009 3:58 pm

Re: basic help with PHP API

Mon Aug 10, 2009 5:11 pm

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...
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7056
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: basic help with PHP API

Mon Aug 10, 2009 5:23 pm

look at my post above again, parameters were missing one symbol :)
 
ninet
just joined
Topic Author
Posts: 14
Joined: Mon Aug 10, 2009 3:58 pm

Re: basic help with PHP API

Tue Aug 11, 2009 1:39 pm

well, still nothing :(... 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.
 
ninet
just joined
Topic Author
Posts: 14
Joined: Mon Aug 10, 2009 3:58 pm

Re: basic help with PHP API

Tue Aug 11, 2009 5:20 pm

it works finally!

topic: http://forum.mikrotik.com/viewtopic.php?f=9&t=22763 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.
 
christina2009
just joined
Posts: 1
Joined: Wed Aug 12, 2009 5:17 am

Re: basic help with PHP API

Wed Aug 12, 2009 5:22 am

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

comparatif simulation taux pret auto - taux pret auto differe selon la prise en compte ... calculent automatiquement le taux pour un prêt automobile donne.comparatif simulation taux pret auto
 
ninet
just joined
Topic Author
Posts: 14
Joined: Mon Aug 10, 2009 3:58 pm

Re: basic help with PHP API

Wed Aug 12, 2009 2:34 pm

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 :)...

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.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: basic help with PHP API

Wed Aug 12, 2009 7:37 pm

this topic is about PHP API implementation - please create new topic for SSH question...
 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Mon Aug 16, 2010 8:59 pm

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/viewtopic.php ... 98#p222798
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: basic help with PHP API

Mon Aug 16, 2010 9:41 pm

I had the same problem
the same problem = the same solution!
 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Mon Aug 16, 2010 10:11 pm

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?
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: basic help with PHP API

Mon Aug 16, 2010 11:46 pm

 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Tue Aug 17, 2010 2:32 pm

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?
 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Tue Aug 17, 2010 3:31 pm

Chupaka´s solution is here: http://forum.mikrotik.com/viewtopic.php ... 31#p222931

The code is working! Visit the link. Thx!
 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Wed Aug 18, 2010 9:43 pm

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/viewtopic.php ... 91#p223191
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: basic help with PHP API

Thu Aug 19, 2010 12:28 am

automatically - no. you may set new password for user periodically, but you cannot logout connected user
 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Fri Aug 27, 2010 9:32 pm

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/viewtopic.php ... 91#p223191


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.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: basic help with PHP API

Sun Aug 29, 2010 9:18 pm

yep, something like this =)

unfortunately, if user is logged in and you remove the user, it's not logged out automatically...
 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Fri Sep 03, 2010 7:24 pm

Chupaka,

Do you know anything about it:

http://forum.mikrotik.com/viewtopic.php?f=9&t=44744

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

Thx.
 
qodirly
just joined
Posts: 1
Joined: Sat Oct 02, 2010 7:05 am

Re: basic help with PHP API

Sat Oct 02, 2010 8:32 am

i'm traying this code:
   $API->write("/ip/hotspot/user/add
   =server=hotspot1 
   =name=john
   =password=summer 
   =profile=default 
   =email=john985@gmail.com
   =comment=honkong
   ");
I enter the command twice,

and the output from the browser:

Array ( [!trap] => Array ( [0] => Array ( [message] => failure: already have user with this name for this server ) ) )


how if the message would be "alert box"?

I tried like this but not successful,

if (print_r($ARRAY) = "Array ( [!trap] => Array ( [0] => Array ( [message] => failure: already have user with this name for this server ) ) )")
{
echo "<script type="text/javascript">window.alert("name already exist")</script>";
} else {
echo "<script type="text/javascript">window.alert("sucsess")</script>";
}

and i also tried
echo "name already exist";

runing but if statement not suitable

please help.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: basic help with PHP API

Mon Oct 04, 2010 4:05 pm

probably something like this:
if (isset($ARRAY['!trap'][0]['message']) and ($ARRAY['!trap'][0]['message'] == 'failure: already have user with this name for this server')) {
  echo "exists";
} else {
  echo "success";
}
p.s. your
echo "<script type="text/javascript">window.alert("name already exist")</script>";
should be
echo "<script type=\"text/javascript\">window.alert(\"name already exist\")</script>";
 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Tue Dec 14, 2010 2:04 pm

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.


I have the same problem but for add a task in scheduller: http://forum.mikrotik.com/viewtopic.php?f=13&t=47455

In version 3.22 of ROS this command cli work:
/system scheduler add name="test" start-date="Aug/26/2010" start-time="17:00:00" interval="00:01:00" on-event="/user  set  password=84d50a  manut;  /system  scheduler remove  abrir_ada749";
But with API PHP not. I don´t know why.

The debug true don´t showed error message and in the scheduller (winbox) don´t show the tasks:
Connection attempt #1 to 111.111.111.111:1111...
<<< [6] /login
>>> [5/5 bytes read.
>>> [5, 39] !done
>>> [37/37 bytes read.
>>> [37, 1] =ret=f40e2d131fecd171fdc3e8b1bc225dd2
<<< [6] /login
<<< [11] =name=admin
<<< [44] =response=00a11dcdc4d92b8bcfa6e6fe8047d4c375
>>> [5/5 bytes read.
>>> [5, 1] !done
Connected...
<<< [17] /system/clock/set
<<< [33] =time-zone-name=America/Sao_Paulo
>>> [5/5 bytes read.
>>> [5, 1] !done

<<< [17] /system/clock/set
<<< [17] =date=Dec/14/2010
>>> [5/5 bytes read.
>>> [5, 1] !done
<<< [17] /system/clock/set
<<< [14] =time=09:45:34
>>> [5/5 bytes read.
>>> [5, 1] !done
<<< [21] /system/scheduler/add
<<< [18] =name=abrir_9d3949

<<< [23] =start-date=Dec/14/2010
<<< [20] =start-time=21:00:00
<<< [18] =interval=00:01:00
<<< [25] =policy=read,write,policy
<<< [81] =on-event=/user set password=f32a52  manut; /system scheduler remove abrir_9d3949
>>> [5/5 bytes read.
>>> [5, 10] !done
>>> [8/8 bytes read.
>>> [8, 1] =ret=*15
<<< [21] /system/scheduler/add

<<< [19] =name=fechar_9d3949
<<< [23] =start-date=Dec/14/2010
<<< [20] =start-time=21:30:00
<<< [18] =interval=00:01:00
<<< [25] =policy=read,write,policy
<<< [82] =on-event=/user set password=876443  manut; /system scheduler remove fechar_9d3949
>>> [5/5 bytes read.
>>> [5, 10] !done
>>> [8/8 bytes read.
>>> [8, 1] =ret=*16
Disconnected...


The same code work fine in ROS 4.5 either through the API or the Winbox terminal.

Thx for any help.


The code i used:
$API->write('/system/clock/set', false);
$API->write('=time-zone-name='.$regiao);
$ARRAY = $API->read();
								
$API->write('/system/clock/set', false);
$API->write('=date='.$data_servidor);
$ARRAY = $API->read(); 
								
$API->write('/system/clock/set', false);
$API->write('=time='.$hora);
$ARRAY = $API->read();
								
// Configurando o agendamento de ABERTURA
$interval = '00:01:00';
$politica = 'read,write,policy';
$comando = '/user set password='.$new_password.'  manut; /system scheduler remove '.$tarefa;
				
$API->write('/system/scheduler/add', false);
$API->write('=name='.$tarefa, false);
$API->write('=start-date='.$dt_mikrotik, false);
$API->write('=start-time='.$hr_form_inicial, false);
$API->write('=interval='.$interval, false);
$API->write('=policy='.$politica, false);
$API->write('=on-event='.$comando);
			
$ARRAY = $API->read();
 
Christiano
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Fri Aug 13, 2010 8:53 pm

Re: basic help with PHP API

Mon Dec 20, 2010 7:11 pm

I can when i replace the user´s name:

Before (don´t function in old version):
$comando = '/user set password='.$new_password.'  manut; /system scheduler remove '.$tarefa;

After (work fine in old and new version):
$comando = '/user set manut password='.$new_password; /system scheduler remove '.$tarefa;
I assume the position of the controls affect the syntax. That is it? I think yes. :)

Who is online

Users browsing this forum: No registered users and 31 guests