Community discussions

MikroTik App
 
anishpsla
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Mon Aug 25, 2014 9:16 am

Mikrotik PHP API probelm

Sat Feb 06, 2016 11:10 am

I am developing a PHP application using Mikrotik API for customer self registration. I am facing a weird problem. I can create a user using PHP scriot. The problem is, some of the users doesn't have any profile with username. Also, deleting those users by API also not possible. I am expecting expert advice in this matter.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Mikrotik PHP API probelm

Sat Feb 06, 2016 12:12 pm

Does the same problem also occur from terminal? Which RouterOS version are you using? What's an example of a command call that fails?
 
anishpsla
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Mon Aug 25, 2014 9:16 am

Re: Mikrotik PHP API probelm

Sun Feb 07, 2016 5:47 pm

Than you for your help. The commands working without any problem through terminal. The problem occurring randomly when done through PHP script. I am using latest version of Router OS.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Mikrotik PHP API probelm

Sun Feb 07, 2016 10:37 pm

Some sample code (that fails (randomly?!?; that's another thing...)) please?
 
anishpsla
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Mon Aug 25, 2014 9:16 am

Re: Mikrotik PHP API probelm

Mon Feb 08, 2016 8:47 am

$API = new RouterosAPI();

		if ($API->connect()) 
		{
	    //Delete existing user. This will fail if the user doesn't have a profile	
            $DelUser = $API->comm("/tool/user-manager/user/remove", array(
								          		"numbers"  => $roomnop,));
            
			$newUser = $API->comm("/tool/user-manager/user/add", array(
						          "customer" => "admin",
						          "username" => $roomnop,
						          "password" => $passwordp,));
			$add_user_profile = $API->comm("/tool/user-manager/user/create-and-activate-profile", array(
												"customer" => "admin",
								          		"numbers"  => $roomnop,
								          		"profile"  => $profilep,));
                $API->disconnect();  
		}
Hi, this is the sample code. The code randomly failed to add profile to a user. For example, the code is creating a user with username 'X' and profile 'Y'. Some time, the user doesn't have a profile. Another problem is, those user created by the PHP code without a profile, can't be deleted using API+PHP code. We want to delete those users using console.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Mikrotik PHP API probelm

Mon Feb 08, 2016 3:21 pm

$roomnop sounds like "room number"... as in, an integer... is it? Although User Manager allows that, I think that part might be error prone. You may want to prefix the username with something, so that it doesn't start with a number... say "room-".

Also, I'm pretty sure create-and-activate-profile requires a profile. If a user "doesn't have a profile" (as in "they aren't limited in any way"), create a profile that doesn't have any limitations, and use that. Alternatively, don't call create-and-activate-profile at all.


For the "random" part... I suggest you try the client from my signature. I'm not aware of it having any randomly occurring problems - it should always fail to create-and-activate-profile when the profile is not there, or if that's allowed - always succeed... Unless it's a RouterOS issue, which in this case I doubt.

Rewriting your code with it in mind:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

//Define $roomnop, $password and $profilep here

try {
    $client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

    $removeRequest = new RouterOS\Request('/tool/user-manager/user/remove');
    $removeRequest->setArgument('numbers', $roomnop)
    $DelUser = $client->sendSync($removeRequest);
    
    $addRequest = new RouterOS\Request('/tool/user-manager/user/add');
    $addRequest
        ->setArgument('customer', 'admin')
        ->setArgument('username', $roomnop)
        ->setArgument('password', $password);
    $newUser = $client->sendSync($addRequest);

    $activateRequest = new RouterOS\Request('/tool/user-manager/user/create-and-activate-profile');
    $activateRequest
        ->setArgument('customer', 'admin')
        ->setArgument('username', $roomnop)
        ->setArgument('profile', $profilep);
    $add_user_profile = $client->sendSync($activateRequest);
} catch (Exception $e) {
} 
 
anishpsla
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Mon Aug 25, 2014 9:16 am

Re: Mikrotik PHP API probelm

Mon Feb 08, 2016 7:04 pm

All usernames are integers. It's a self-care system. The guest want to create a user with the help of reception (For unique ID). Also the profile fields are mandatory. So the problem is either related to the PHP script or the API or even Router OS itself.

What might be the reason for inability to delete a user (Which doesn't have a profile) using the PHP code ? If you can suggest a way to delete such users, then my problem is solved.

Your library look cool and easy to use. Unfortunately, I can't rewrite the whole code as I have some difficulties for that, and the time to finish the project is already over.
Thanks for your reply.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Mikrotik PHP API probelm

Mon Feb 08, 2016 8:36 pm

If you have some JavaScript in your form that does something with the profile part, it could be a browser issue (incompatibility with some JavaScript thing), or perhaps even a timing issue (device is too slow/fast for what JavaScript expects at a point).

A quick fix would be to simply check whether the profile is set when receiving, and specify a default profile when it isn't, e.g.
if (empty($_POST['profile']) || !in_array($_POST['profile'], array('default', 'slow', 'fast'))) {
    $profilep = 'default';
} else {
    $profilep = $_POST['profile'];
}
(assuming valid profile names are "default", "slow" and "fast")
Last edited by boen_robot on Tue Feb 09, 2016 3:48 pm, edited 1 time in total.
 
anishpsla
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Mon Aug 25, 2014 9:16 am

Re: Mikrotik PHP API probelm

Tue Feb 09, 2016 11:35 am

First, thanks for your code. The software have client side validation for selecting a profile. Without selecting a profile, the user can't register.

Another problem, why I can't delete a user using API, who have no profile.
 
anishpsla
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Mon Aug 25, 2014 9:16 am

Re: Mikrotik PHP API probelm

Fri Feb 12, 2016 4:33 pm

Any help from experts ?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Mikrotik PHP API probelm

Fri Feb 12, 2016 6:10 pm

First, thanks for your code. The software have client side validation for selecting a profile. Without selecting a profile, the user can't register.
Client side validation can be bypassed or "missed", so you should have the above code regardless.
Another problem, why I can't delete a user using API, who have no profile.
Could be a RouterOS bug. One way to workaround it is to call a "print" with a query that tests for the username. Extract the ".id" out of the match, and use that as the value for "numbers".
 
anishpsla
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Mon Aug 25, 2014 9:16 am

Re: Mikrotik PHP API probelm

Tue Feb 23, 2016 1:42 pm

Can't get 'id' for a user.
 print detail where username =510H
 
 0     customer=admin username="510H" password="321" shared-users=1 wireless-psk="" wireless-enc-key="" wireless-enc-algo=none uptime-used=1d33s download-used=1649974649 upload-used=133415505 last-seen=feb/10/2016 14:30:47 

Please let me know how to get 'id' for a user.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Mikrotik PHP API probelm

Tue Feb 23, 2016 4:58 pm

It's ".id" (notice the dot), and it's visible only from API. When I said you should call "print", I meant call it from the API.
 
anishpsla
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Mon Aug 25, 2014 9:16 am

Re: Mikrotik PHP API probelm

Tue Feb 23, 2016 5:20 pm

Ok, thanks for your reply. I will try and post the result.