Community discussions

MikroTik App
 
yulley
just joined
Topic Author
Posts: 4
Joined: Fri Jul 10, 2009 4:49 pm

userman with PHP

Sun Mar 17, 2019 4:47 pm

please help...
i need add user and profile to userman ROS 6.42 with PHP,
for add user success with this script
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util(
    $client = new RouterOS\Client('192.168.88.1', 'admin', 'admin', '8728')
);
$util->setMenu('/tool user-manager user');
$util->add(
    array(
		'customer' => 'admin',
		'disabled' => 'no',
        'username' => 'user1',
		'password' => 'user1',
		'shared-users' => '1',
        'phone' => '+6281345678'
    )
);
but not success add profile with this
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2/Autoload.php';
$util = new RouterOS\Util(
    $client = new RouterOS\Client('192.168.88.1', 'admin', 'admin', '8728')
);
$util->setMenu('/tool user-manager user create-and-activate-profile');
$util->add(
    array(
		'customer' => 'admin',
		'numbers' => '0',
		'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: userman with PHP  [SOLVED]

Mon Mar 25, 2019 12:12 am

"create-and-activate-profile" is a command, not a menu IIRC.

It's not a normal CRUD command, so you'll have to make a request to give to Client, instead of using Util.

Also, numbers should really contain the username (or otherwise the ID returned by finding the customer/username combo). e.g.
$client->sendSync(
    $util->setMenu('/tool user-manager user')->newRequest(
        'create-and-activate-profile',
        array(
	    'customer' => 'admin',
	    'numbers' => 'user1',
	    'profile' => 'profile1'
        )
    )
);
 
AdamGweleg
just joined
Posts: 15
Joined: Wed May 29, 2019 1:34 am

Re: userman with PHP

Fri Jul 05, 2019 2:58 pm

"create-and-activate-profile" is a command, not a menu IIRC.

It's not a normal CRUD command, so you'll have to make a request to give to Client, instead of using Util.

Also, numbers should really contain the username (or otherwise the ID returned by finding the customer/username combo). e.g.
$client->sendSync(
    $util->setMenu('/tool user-manager user')->newRequest(
        'create-and-activate-profile',
        array(
	    'customer' => 'admin',
	    'numbers' => 'user1',
	    'profile' => 'profile1'
        )
    )
);
Im using this code but when i run the code I've noted that the router CPU is rising to 30% and each voucher is taking more than 2 minutes.

My router is RB1100AHx4 dude edition v6.43.16

<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2/Autoload.php';


for ($i = 0;$i < 7500; ){
    set_time_limit(0);
    
   
    $lines = file('add.txt');
       
    foreach ($lines as $line){

if ($i<7500){


    $util = new RouterOS\Util(
        $client = new RouterOS\Client('192.168.1.1', 'admin', '12345')
    );
    
    $voucher = (int)$line;
    $util->setMenu('/tool user-manager user');
    $util->add(
    
        array(
            'customer'=>'admin',
            'disabled'=>'no',
            'password' => '1234',
            'shared-users' => '1',
            'username' => $voucher,
            'wireless-enc-algo'=>'none',
            'wireless-enc-key'=>'',
            'wireless-psk'=>''
            // 'copy-from' =>'master1d'
        )
        );

     $client->sendSync($util->setMenu('/tool user-manager user')-> newRequest('create-and-activate-profile', [

        'numbers' => $voucher,
        'customer' => 'admin',
        'profile' => '1D'
     ]));



    $i++;
    echo "numer of vouchers is: ". $i. "<br>";
    echo" VoucherAdded" ." ".$line."<br>";
}




        
    }
    

    
    }

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

Re: userman with PHP

Sat Jul 06, 2019 4:21 pm

What you're doing is that for every iteration in your loop, you're making a brand new connection, login to the router once again. This is a huge waste of resources.

Move the new RouterOS\Util and new RouterOS\Client outside of the loop, so that you keep using the same connection for all vouchers.

Also, you have two loops, with the "for" one not really serving its purpose. Remove it. You're already making the 7500 check inside the foreach anyway... i.e. only the first 7500 lines in your file would result in vauchers.
 
AdamGweleg
just joined
Posts: 15
Joined: Wed May 29, 2019 1:34 am

Re: userman with PHP

Sat Jul 06, 2019 6:11 pm

What you're doing is that for every iteration in your loop, you're making a brand new connection, login to the router once again. This is a huge waste of resources.

Move the new RouterOS\Util and new RouterOS\Client outside of the loop, so that you keep using the same connection for all vouchers.

Also, you have two loops, with the "for" one not really serving its purpose. Remove it. You're already making the 7500 check inside the foreach anyway... i.e. only the first 7500 lines in your file would result in vauchers.
Thank you boen_robot

Today morning I've upgraded my router to the latest stable version because I thought the problem with the router version, and now when I'm trying to connect to the router using API I'm getting a username or password error. 😔

and I'm sure of the credentials are fine because i can log in through inbox.

Any help please??
 
yulley
just joined
Topic Author
Posts: 4
Joined: Fri Jul 10, 2009 4:49 pm

Re: userman with PHP

Tue Jul 09, 2019 11:38 am

Thank you very much and work with your script
so now i have new problem after added new profile, the billing/time left session will start and pause if any profile before and active in session.
maybe any script just add new profile without activated new profile (new profile will active after previous profile ended).

* i'm sorry my english not good
"create-and-activate-profile" is a command, not a menu IIRC.

It's not a normal CRUD command, so you'll have to make a request to give to Client, instead of using Util.

Also, numbers should really contain the username (or otherwise the ID returned by finding the customer/username combo). e.g.
$client->sendSync(
    $util->setMenu('/tool user-manager user')->newRequest(
        'create-and-activate-profile',
        array(
	    'customer' => 'admin',
	    'numbers' => 'user1',
	    'profile' => 'profile1'
        )
    )
);
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: userman with PHP

Mon Jul 15, 2019 12:24 am

Hello,
I know this an old post. Please i need an assistance on this issue. I am building a database using my php. I want to use a script to add these users from the php database to Userman.
Through google and this forum, i have downloaded Infinitel Hotspot Auto service but to no avail. I have used pearl but to no avail.
see my script below

<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
//$db = mysqli_connect('localhost','root','','students');
$client = new RouterOS\Client('192.168.2.2', 'admin', 'password@1', '8728');
$mysqli = new mysqli('localhost', 'root', '', 'students');

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


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


//die(var_dump($users->fetch_object()));
while ($user = $users->fetch_object()) {
$addRequest
->setArgument('name', $user->username)
->setArgument('password', $user->password)
->setArgument('customer', 'admin')
->setArgument('profile', 'staff');
$client($addRequest);
// var_dump($addRequest);
}
?>
 
onyegbadocu
newbie
Posts: 25
Joined: Wed Nov 22, 2017 12:49 pm

Re: userman with PHP

Sat Jul 20, 2019 2:33 pm

please help...
i need add user and profile to userman ROS 6.42 with PHP,
for add user success with this script
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util(
    $client = new RouterOS\Client('192.168.88.1', 'admin', 'admin', '8728')
);
$util->setMenu('/tool user-manager user');
$util->add(
    array(
		'customer' => 'admin',
		'disabled' => 'no',
        'username' => 'user1',
		'password' => 'user1',
		'shared-users' => '1',
        'phone' => '+6281345678'
    )
);
but not success add profile with this
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2/Autoload.php';
$util = new RouterOS\Util(
    $client = new RouterOS\Client('192.168.88.1', 'admin', 'admin', '8728')
);
$util->setMenu('/tool user-manager user create-and-activate-profile');
$util->add(
    array(
		'customer' => 'admin',
		'numbers' => '0',
		'profile' => 'profile1'
    )
);
I need assistance . i cant load Pear2\Autoloader. php.I get an error that there is a missing directory.
 
wawananakkaili
just joined
Posts: 1
Joined: Sat Jan 04, 2020 2:16 pm

Re: userman with PHP

Sat Jan 11, 2020 8:22 am

Regards... my First Question... Please Help me...

i was modified every script in this forum for make this working. it was succed in the localhost. but didn't working in the real host... i was user VPS as Userman Server...

Controller :
function __construct(){
parent::__construct();
require_once(APPPATH.'libraries/PEAR2/Autoload.php');
}

function edit(){

try {
$util = new PEAR2\Net\RouterOS\Util($client = new PEAR2\Net\RouterOS\Client('103.xxx.xxx.xxx', 'user', 'password'));
} catch (Exception $e) {
// redirect when failed
redirect(base_url().'admin/pelanggan/data?alert=gagal_konek_userman');
}

}`
and there... always failed...

File detail :
host\application\libraries\PEAR2\Autoload

please... help me... and Thanks Before
Last edited by wawananakkaili on Sat Jan 11, 2020 8:33 am, edited 1 time in total.

Who is online

Users browsing this forum: No registered users and 31 guests