Community discussions

MikroTik App
 
didithilmy
just joined
Topic Author
Posts: 14
Joined: Sun Aug 04, 2013 1:30 pm

Adding User to Userman via PHP

Sun Aug 04, 2013 1:37 pm

Hi
I'm currently using User Manager for AAA in MikroTIK RouterOS v6 right now. My organization will have about 300+ existing user and all stored in a MySQL Database. It would be difficult to add it one by one. My plan is to add the users using PHP script. Is there anyway to add Userman user using PHP API?
Thank You
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Mon Aug 05, 2013 1:49 pm

Sure. There are at least 3 ways (as in "PHP clients") in fact. I'd reccomend the one from my signature, for obvious reasons.

Assuming you have a DB table called "users" with "username" and "password" columns (where the password is NOT hashed), you can do it like:
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
$mysqli = new mysqli('localhost', 'root', '', 'db');

$addRequest = new RouterOS\Request('/tool user-manager user add');

$users = $mysqli->query('SELECT `username`, `password` FROM `users`');

while ($user = $users->fetch_object()) {
    $addRequest
        ->setArgument('name', $user->username)
        ->setArgument('password', $user->password);
    $client($addRequest);
} 
If the passwords in your DB are hashed (as they should've been BTW), you'll have to either ask users to reenter their password (which you can do transparently at your current login form, after warning them they have to login at least once within a certain time window before their account becomes invalid), which you can then enter in the router OR you could generate a random password that you somehow then give to each user, and let them change it once they are logged in.
 
didithilmy
just joined
Topic Author
Posts: 14
Joined: Sun Aug 04, 2013 1:30 pm

Re: Adding User to Userman via PHP

Mon Aug 05, 2013 4:28 pm

Great! Looking forward to try that one.
Thanks for your reply. It helps so much
 
didithilmy
just joined
Topic Author
Posts: 14
Joined: Sun Aug 04, 2013 1:30 pm

Re: Adding User to Userman via PHP

Mon Aug 05, 2013 4:30 pm

Thanks! I will try that soon.
 
didithilmy
just joined
Topic Author
Posts: 14
Joined: Sun Aug 04, 2013 1:30 pm

Re: Adding User to Userman via PHP

Wed Aug 07, 2013 7:49 am

Besides username and password, any other arguments? For example, is there argument to define which group the user belongs to?

Thank You
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Wed Aug 07, 2013 1:29 pm

I'm not really sure, as I'm not using the user-manager myself.

You can open up a terminal window, then type "/tool user-manager user add ?" to see what each user can have. In general, you can hit "?" from a terminal window to see possiblities from your current point onwards.

AFAIK, each user can be associated with a "customer", and one customer can have many users... the idea being that the "customer" relates to "who's paying" while the "user" relates to "who's logging in" (e.g. one person paying for two user names).
 
didithilmy
just joined
Topic Author
Posts: 14
Joined: Sun Aug 04, 2013 1:30 pm

Re: Adding User to Userman via PHP

Wed Aug 07, 2013 2:40 pm

Ok, thank you for your information
 
didithilmy
just joined
Topic Author
Posts: 14
Joined: Sun Aug 04, 2013 1:30 pm

Re: Adding User to Userman via PHP

Mon Aug 19, 2013 6:01 am

By the way, which password should be used, WebFig or Userman?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Mon Aug 19, 2013 2:19 pm

You mean for login to the API?

The RouterOS login passwords... you know, those at the "/user" menu. I believe they're shared by WebFig.
 
didithilmy
just joined
Topic Author
Posts: 14
Joined: Sun Aug 04, 2013 1:30 pm

Re: Adding User to Userman via PHP

Wed Aug 21, 2013 1:37 pm

It does not work, maybe the command is wrong?
Thank you
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Wed Aug 21, 2013 1:41 pm

Does the part up to
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

$client = new RouterOS\Client('192.168.0.1', 'admin', 'password'); 
work? Are there any error messages in PHP, or is it working fine, but without the effect?

If you're not sure, add some "try{} catch{}", e.g.
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

try {
    $client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
    $mysqli = new mysqli('localhost', 'root', '', 'db');

    $addRequest = new RouterOS\Request('/tool user-manager user add');

    $users = $mysqli->query('SELECT `username`, `password` FROM `users`');

    while ($user = $users->fetch_object()) {
        $addRequest
            ->setArgument('name', $user->username)
            ->setArgument('password', $user->password);
        $client($addRequest);
    }
} catch (RouterOS\SocketException $e) {
    echo 'Connection to RouterOS failed... ' . $e;
} catch (RouterOS\DataFlowException $e) {
    echo $e->getMessage();//Wrong username or password; probably
} catch (Exception $e) {
    echo 'Unknown exception... ' . $e; //Connection fail to MySQL; probably
} 



EDIT: Wait... I had installed user-manager since my last reply, and now I can see that to add a user, you MUST associate it with a customer, so THAT is the problem.
Last edited by boen_robot on Wed Aug 21, 2013 1:45 pm, edited 1 time in total.
 
didithilmy
just joined
Topic Author
Posts: 14
Joined: Sun Aug 04, 2013 1:30 pm

Re: Adding User to Userman via PHP

Wed Aug 21, 2013 1:43 pm

It worked fine, thank you. Apparently the Userman console didn't directly refreshes its users list.
 
chuddy
just joined
Posts: 8
Joined: Mon Aug 26, 2013 6:21 pm

Re: Adding User to Userman via PHP

Fri Apr 18, 2014 4:51 pm

Hi,
I wish to create user accounts in Mikrotik user manager using a PHP API, I followed your above instruction, applied the code snipet provided above and got a load of error. kindly look at this and advise me.

Connection to RouterOS failed... exception 'PEAR2\Net\Transmitter\SocketException' with message 'Failed to connect with socket.' in phar://C:/xampp/htdocs/switchonwifi/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/Transmitter/TcpClient.php:199 Stack trace: #0 phar://C:/xampp/htdocs/switchonwifi/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/Transmitter/TcpClient.php(160): PEAR2\Net\Transmitter\TcpClient->createException('Failed to conne...', 8) #1 phar://C:/xampp/htdocs/switchonwifi/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/RouterOS/Communicator.php(149): PEAR2\Net\Transmitter\TcpClient->__construct('192.168.88.1', 8728, false, NULL, 'admin/eketapwd', '', NULL) #2 phar://C:/xampp/htdocs/switchonwifi/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/RouterOS/Client.php(142): PEAR2\Net\RouterOS\Communicator->__construct('192.168.88.1', 8728, false, NULL, 'admin/eketapwdpwd', '', NULL) #3 C:\xampp\htdocs\switchonwifi\library\common.php(172): PEAR2\Net\RouterOS\Client->__construct('192.168.88.1', 'admin', 'eketapwd') #4 C:\xampp\htdocs\switchonwifi\library\common.php(136): common->addtoMikrotik('08010310', '123') #5 C:\xampp\htdocs\switchonwifi\library\common.php(69): common->createAccount(0) #6 C:\xampp\htdocs\switchonwifi\library\common.php(56): common->getModule('22af645d1859cb5...') #7 C:\xampp\htdocs\switchonwifi\index.php(15): common->getPages('p') #8 {main} Next exception 'PEAR2\Net\RouterOS\SocketException' with message 'Error connecting to RouterOS' in phar://C:/xampp/htdocs/switchonwifi/PEAR2_Net_RouterOhtS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/RouterOS/Communicator.php:151 Stack trace: #0 phar://C:/xampp/htdocs/switchonwifi/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/RouterOS/Client.php(142): PEAR2\Net\RouterOS\Communicator->__construct('192.168.88.1', 8728, false, NULL, 'admin/eketapwd', '', NULL) #1 C:\xampp\htdocs\switchonwifi\library\common.php(172): PEAR2\Net\RouterOS\Client->__construct('192.168.88.1', 'admin', 'eketapwd') #2 C:\xampp\htdocs\switchonwifi\library\common.php(136): common->addtoMikrotik('08010310', '123') #3 C:\xampp\htdocs\switchonwifi\library\common.php(69): common->createAccount(0) #4 C:\xampp\htdocs\switchonwifi\library\common.php(56): common->getModule('22af645d1859cb5...') #5 C:\xampp\htdocs\switchonwifi\index.php(15): common->getPages('p') #6 {main}

I truly need to get this right.

Thanks alot.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Fri Apr 18, 2014 5:02 pm

Check your firewall settings. It must allow php-cgi.exe and/or Apache's "httpd.exe" to make outgoing connections.
 
chuddy
just joined
Posts: 8
Joined: Mon Aug 26, 2013 6:21 pm

Re: Adding User to Userman via PHP

Fri Apr 18, 2014 5:28 pm

I think my firewall settings are ok and should allow this action, but what specific settings do you think I should put in place?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Fri Apr 18, 2014 5:53 pm

Like I said, whitelist php-cgi.exe and httpd.exe to make outgoing connections to any ip/port they want. That's the setting(s) you need to make. Here's how.

Alternatively, to make sure it's not a firewall issue, consider temporarily turning off your firewall altogether. If you have an Antivirus that includes a firewall, turn that off too.

If it's not a firewall issue, the next thing to check is that you have enabled the API service in RouterOS (under "/ip service").
 
chuddy
just joined
Posts: 8
Joined: Mon Aug 26, 2013 6:21 pm

Re: Adding User to Userman via PHP

Fri Apr 18, 2014 6:38 pm

I have enable API, and the errors have stop. But, I just can not find the users I created in the User list of Mikrotik Usermanager.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Fri Apr 18, 2014 7:21 pm

As I noted in my last post before you showed up,
EDIT: Wait... I had installed user-manager since my last reply, and now I can see that to add a user, you MUST associate it with a customer, so THAT is the problem.
So... add a customer first, and include its ID into a "customer" argument.

The code above was written with version b3 of my client in mind. In b4, you can also do it as:
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$util->changeMenu('/tool user-manager customer');
$customerId = $util->add(array('login' => 'customer1'));

$util->changeMenu('.. user');
$util->add(array('customer' => $customerId, 'name' => 'username1')); 
 
chuddy
just joined
Posts: 8
Joined: Mon Aug 26, 2013 6:21 pm

Re: Adding User to Userman via PHP

Wed Apr 23, 2014 10:20 pm

Thanks for the update, but I tried it using standard parameters but it didnt register a user. the code is below:
 $tdata=800*(1024*1024);
            $upl=800*(1024*1024);
           $util = new RouterOS\Util($client = new RouterOS\Client('xxx.xxx.xx.xxx', 'xxxx', 'xxxx'));
           $util->changeMenu('/tool user-manager customer');
            $customerId = $util->add(array('login' => 'customer1'));

            $util->changeMenu('.. user');
            $util->add(array('customer' => $customerId, 'name' =>$usr, 'password'=>$pwd, 
                        'limit-bytes-out'=>$tdata,'limit-bytes-in'=>$tdata, 
                        'limit-bytes-total'=>$tdata,'limit-uptime'=> '2w'
                )); 
All I want to do is create users, assign them to a Profile, or directly set their time and data limits.
Looking forward to your assistance. Thanks
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Thu Apr 24, 2014 1:33 pm

User-manager is not the same as hotspot.

Looking at the help of the "add" command, I don't see it having any of those "limit-*" arguments.

Check out valid values from terminal by typing "?" after the command, e.g.

ros code

/tool user-manager user add ?
As for how do you set such limits when using user-manager in general... I don't know.
 
chuddy
just joined
Posts: 8
Joined: Mon Aug 26, 2013 6:21 pm

Re: Adding User to Userman via PHP

Thu Apr 24, 2014 6:07 pm

Please, How can I perform the following operation with PHP

[admin@MikroTik] ip hotspot user> add name=ex password=ex \
\... mac-address=01:23:45:67:89:AB limit-uptime=1h
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Thu Apr 24, 2014 6:14 pm

Same way as with user-manager (but with the arguments tweaked accordingly, of course).

e.g.
$util->changeMenu('/ip hotspot user');
$util->add(
    array(
        'name' => 'ex',
        'password' => 'ex',
        'mac-address' => '01:23:45:67:89:AB',
        'limit-uptime' => '1h'
    )
); 
or alternatively
$client->sendSync(
    new RouterOS\Request(
        '/ip hotspot user add name=ex password=ex mac-address=01:23:45:67:89:AB limit-uptime=1h'
    )
); 
 
gtblue
just joined
Posts: 8
Joined: Thu May 26, 2016 4:20 am

Re: Adding User to Userman via PHP

Sun Jun 05, 2016 11:44 am

Same way as with user-manager (but with the arguments tweaked accordingly, of course).

e.g.
$util->changeMenu('/ip hotspot user');
$util->add(
    array(
        'name' => 'ex',
        'password' => 'ex',
        'mac-address' => '01:23:45:67:89:AB',
        'limit-uptime' => '1h'
    )
);
or alternatively
$client->sendSync(
    new RouterOS\Request(
        '/ip hotspot user add name=ex password=ex mac-address=01:23:45:67:89:AB limit-uptime=1h'
    )
);

hai boen,

sorry if i also ask you in another person posting. i just curious how to use those script to insert bunch of user into usermanager. honestly im pretty blind with scripting. perhaps you could teach me, where do i have to start. i'll appreaciate all your help. thank you
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Sun Jun 05, 2016 3:56 pm

hai boen,

sorry if i also ask you in another person posting. i just curious how to use those script to insert bunch of user into usermanager. honestly im pretty blind with scripting. perhaps you could teach me, where do i have to start. i'll appreaciate all your help. thank you
I suggest you learn PHP in general. There are a lot of sites out there (the one I used myself was W3Schools), including also the PHP manual itself (though if you don't know any programming language, that one is not as suitable).

The most difficult part of learning PHP is installing it. While it becomes second nature once you do it (or if you've installed other similar runtimes), the first time is daunting. Packages like XAMPP make it easier to get started by providing everything with a simple GUI installer that sets up everything ready to go.


To actually do the insertion, you merely loop over the data you'd be inserting (e.g. if it's in a database, get all the rows, and loop over those; If it's in a text file, get all lines with file(), and loop over them, further separating the individual fields using other functions, etc.). And on each iteration, call $util->add() with the user manager user data into an array, analogously to the above example code.
 
gtblue
just joined
Posts: 8
Joined: Thu May 26, 2016 4:20 am

Re: Adding User to Userman via PHP

Tue Jun 07, 2016 12:26 pm

hai boen,

sorry if i also ask you in another person posting. i just curious how to use those script to insert bunch of user into usermanager. honestly im pretty blind with scripting. perhaps you could teach me, where do i have to start. i'll appreaciate all your help. thank you
I suggest you learn PHP in general. There are a lot of sites out there (the one I used myself was W3Schools), including also the PHP manual itself (though if you don't know any programming language, that one is not as suitable).

The most difficult part of learning PHP is installing it. While it becomes second nature once you do it (or if you've installed other similar runtimes), the first time is daunting. Packages like XAMPP make it easier to get started by providing everything with a simple GUI installer that sets up everything ready to go.


To actually do the insertion, you merely loop over the data you'd be inserting (e.g. if it's in a database, get all the rows, and loop over those; If it's in a text file, get all lines with file(), and loop over them, further separating the individual fields using other functions, etc.). And on each iteration, call $util->add() with the user manager user data into an array, analogously to the above example code.

Dear Boen,

many thanks for your advise, actually i've been deal with xampp couple of time, but like what you said earlier i will try to learn deeply again about php it self to be used on the script for mikrotik.
if you dont mind, while im learning what im doing, i might need your guidance.
once more thank your for your quick guidance.
 
uchepercynnoch
just joined
Posts: 7
Joined: Tue Jul 25, 2017 7:42 pm

Re: Adding User to Userman via PHP

Tue Jul 25, 2017 7:52 pm

Hello boen_robot,

Please how do I enable a hotspot user and generate voucher with PEAR2_Net_RouterOS
Thanks
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Tue Jul 25, 2017 11:44 pm

The question is how would you do it "manually"?

For enabling an existing, but disabled, hotspot user:
$util->changeMenu('/ip hotspot user')->enable('user1');
(replacing "user1" with the name of the user you want to enable)

And again... How would you generate a voucher "manually"?
 
uchepercynnoch
just joined
Posts: 7
Joined: Tue Jul 25, 2017 7:42 pm

Re: Adding User to Userman via PHP

Thu Jul 27, 2017 10:21 am

A million thanks boen_robot. To generate vouchers manually, I have to do it manually from the usermanager by selecting the hotspot user and clicking the 'generate voucher button'.
But I dont want to do this manually. Is there a command to do this using the PEAR_NET_RouterOS php API? I do appreciate if you can help me out.
Thanks.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Sat Jul 29, 2017 6:51 pm

The API protocol matches the RouterOS CLI in terms of capabilities. And you can't generate vouchers from CLI, so therefore you can't make vouchers from CLI either.

You'd need to replicate the individual steps as part of your PHP application, and use the API where applicable. i.e. use PHP to generate the voucher data, and use the API to set the credentials and/or other details accordingly.
 
uchepercynnoch
just joined
Posts: 7
Joined: Tue Jul 25, 2017 7:42 pm

Re: Adding User to Userman via PHP

Sat Aug 05, 2017 6:09 pm

Thanks a lot, I had that in mind...however i m trying to add a new user to my hotspot with the API, but I got the following error:

Fatal error: Uncaught exception 'PEAR2\Net\RouterOS\RouterErrorException' with message 'Router returned error when adding items' in phar://C:/wamp64/www/mikrotik_dev/vendor/PEAR2_Net_RouterOS-1.0.0b6.phar/PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Net/RouterOS/Util.php:867 Stack trace: #0 C:\wamp64\www\mikrotik_dev\src\hotspot\model\Router.php(58): PEAR2\Net\RouterOS\Util->add(Array) #1 C:\wamp64\www\mikrotik_dev\index.php(20): hotspot\model\Router->addUser() #2 {main} Response collection: Array ( [0] => PEAR2\Net\RouterOS\Response Object ( [unrecognizedWords:protected] => Array ( ) [_type:PEAR2\Net\RouterOS\Response:private] => !trap [attributes:protected] => Array ( [message] => input does not match any value of profile ) [_tag:PEAR2\Net\RouterOS\Message:private] => ) [1] => PEAR2\Net\RouterOS\Response Object ( [unrecognizedWords:protected] => Array in phar://C:/wamp64/www/mikrotik_dev/vendor/PEAR2_Net_RouterOS-1.0.0b6.phar/PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Net/RouterOS/Util.php on line 867



here is my code:
<?php

namespace hotspot\model;
use hotspot\config\Config;
use PEAR2\Net\RouterOS;
require_once __DIR__.'/../../../vendor/PEAR2_Net_RouterOS-1.0.0b6.phar';
class Router
{
public function addUser()
{
$util = new RouterOS\Util($client = new RouterOS\Client('192.168.x.x', 'admin', '1234'));
$util->setMenu('/ip hotspot user')->add([
'name' => 'user1',
'password' => 'password1',
'profile' => 'profile1'
]);
}

}

Am I doing something wrong?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Sat Aug 05, 2017 6:55 pm

Well, the error message says "input does not match any value of profile", so... Perhaps there's no user profile named "profile1"?
 
uchepercynnoch
just joined
Posts: 7
Joined: Tue Jul 25, 2017 7:42 pm

Re: Adding User to Userman via PHP

Mon Aug 07, 2017 5:36 pm

Thanks, I did an update on the routerOS and it solved the problem. Please how do I assign a profile to a user in user-manager? seems I cant pass a 'profile' parameter in the add array like so:

public function addUser()
{
$this->_util->setMenu('/tool user-manager user create-and-activate-profile')->add(
[
'username' => 'user1',
'password' => 'password1',
'customer' => 'default',
'profile' => 'profile1'
]
);
}
Last edited by uchepercynnoch on Mon Aug 07, 2017 7:13 pm, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Mon Aug 07, 2017 7:10 pm

User Manager has a separate command for that called "create-and-active-profile" that identifies the user, the customer they belong to, and the profile to assign and activate. All of those must exist beforehand of course.

Util can't call that command on its own, but it can make a request for Client to send. e.g.
$util = new RouterOS\Util($client = new RouterOS\Client('192.168.x.x', 'admin', '1234'));
$client->sendSync($util->setMenu('/tool user-manager user')->newRequest('create-and-activate-profile', [
'numbers' => 'user1',
'customer' => 'customer1',
'profile' => 'profile1'
])); 
 
uchepercynnoch
just joined
Posts: 7
Joined: Tue Jul 25, 2017 7:42 pm

Re: Adding User to Userman via PHP

Mon Aug 07, 2017 7:50 pm

Thanks a lot :D will try it now
 
uchepercynnoch
just joined
Posts: 7
Joined: Tue Jul 25, 2017 7:42 pm

Re: Adding User to Userman via PHP

Wed Aug 16, 2017 5:43 pm

Hello boen_robot,

I am having some issues adding hotspt user to profile. So far, I have been able to create a hotspot user on user-manager and insert into a database. but when i try to add user to profile in mikrotik router user-manager, I get the following error:

( ! ) Fatal error: Uncaught exception 'PEAR2\Net\RouterOS\DataFlowException' with message 'Asynchonous commands must have a tag.' in phar://C:/wamp64/www/mikrotik_dev/vendor/PEAR2_Net_RouterOS-1.0.0b6.phar/PEAR2_Net_RouterOS-1.0.0b6/src/PEAR2/Net/RouterOS/Client.php on line 390

Please help me understand this. Below is my code:

<?php
use hotspot\config\Config;
use hotspot\model\Db;
use hotspot\model\Router;
use hotspot\model\RouterCommand;
use hotspot\controller\RouterQuery;


require_once __DIR__.'/src/includes/bootstrap.php';

$username = $password = $customer = "";

if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['customer']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$customer = $_POST['customer'];

$getClient = Router::startRouter()->getClient();

$input1 = [];
$input2 = [];

$insert = Db::getInstance()->insert('users', [
'user_name' => $username,
'user_pass' => $password,
'user_owner' => $customer
]);

if ($insert)
{
$hotspot = Router::startRouter()->createUser($username, $password, $customer);

if ($hotspot == true)
{
$user = Db::getInstance()->get('users', [
'user_name', '=', $username
]);

if($user !== false && $user->count() > 0)
{
foreach ($user->results() as $user)
{
$input1[] = $user->user_name;
$input2[] = $user->user_owner;
}
}
else
{
echo 'error';
}
$data1 = $input1[0];
$data2 = $input2[0];

if ($data1 == true && $data2 == true)
{
$getClient->sendAsync($hotspot->newRequest('create-and-activate-profile', [
'numbers' => $data1,
'customer' => $data2,
'profile' => 'profile1'
]));
}


}

}

}
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Adding User to Userman via PHP

Wed Aug 16, 2017 8:16 pm

Is there a specific reason you want to use sendAsync() instead of sendSync()? Like a bunch of other API calls you want to make at once maybe?

Although you could use sendAsync() for finite commands, that method is best suited for infinite commands (where you have to explicitly cancel them to stop them). I would recommend using sendSync(), as in the example in the post above.

If you insist on using sendAsync(), then as the error message says, you must provide a tag in the request (4th argument in Util::newRequest(), 3rd being a query; any string will do for a tag). You'll also need to eventually call either loop() or completeRequest() to actually have the router save the data (otherwise, the router just gets ready to save it).

e.g.
$getClient->sendAsync($hotspot->newRequest('create-and-activate-profile', [
'numbers' => $data1,
'customer' => $data2,
'profile' => 'profile1'
], null, 'activator'));
//Some other stuff you want to do before finishing up
$getClient->completeRequest('activator');
 
uchepercynnoch
just joined
Posts: 7
Joined: Tue Jul 25, 2017 7:42 pm

Re: Adding User to Userman via PHP

Thu Aug 17, 2017 12:05 pm

Thanks a lot bro, trying it out now :D
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: Adding User to Userman via PHP

Wed May 10, 2023 10:31 am

Does the part up to
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

$client = new RouterOS\Client('192.168.0.1', 'admin', 'password'); 


work? Are there any error messages in PHP, or is it working fine, but without the effect?

If you're not sure, add some "try{} catch{}", e.g.
[code=php]<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

try {
    $client = new RouterOS\Client('192.168.0.1', 'admin', 'password'); This code is working for version 6.45 and below. It's not working for versions above 6.45.
    is there an updated syntax to logon via api?
    $mysqli = new mysqli('localhost', 'root', '', 'db');

    $addRequest = new RouterOS\Request('/tool user-manager user add');

    $users = $mysqli->query('SELECT `username`, `password` FROM `users`');

    while ($user = $users->fetch_object()) {
        $addRequest
            ->setArgument('name', $user->username)
            ->setArgument('password', $user->password);
        $client($addRequest);
    }
} catch (RouterOS\SocketException $e) {
    echo 'Connection to RouterOS failed... ' . $e;
} catch (RouterOS\DataFlowException $e) {
    echo $e->getMessage();//Wrong username or password; probably
} catch (Exception $e) {
    echo 'Unknown exception... ' . $e; //Connection fail to MySQL; probably
} 



EDIT: Wait... I had installed user-manager since my last reply, and now I can see that to add a user, you MUST associate it with a customer, so THAT is the problem.
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: Adding User to Userman via PHP

Mon May 22, 2023 10:44 pm

does any one know to the code to access router os version 6.45 and above using php?

Who is online

Users browsing this forum: GoogleOther [Bot] and 21 guests