<?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);
}
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
<?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
}
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}
So... add a customer first, and include its ID into a "customer" argument.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.
<?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'));
$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'
));
/tool user-manager user add ?As for how do you set such limits when using user-manager in general... I don't know.
[admin@MikroTik] ip hotspot user> add name=ex password=ex \
\... mac-address=01:23:45:67:89:AB limit-uptime=1h
$util->changeMenu('/ip hotspot user');
$util->add(
array(
'name' => 'ex',
'password' => 'ex',
'mac-address' => '01:23:45:67:89:AB',
'limit-uptime' => '1h'
)
);
$client->sendSync(
new RouterOS\Request(
'/ip hotspot user add name=ex password=ex mac-address=01:23:45:67:89:AB limit-uptime=1h'
)
);
Same way as with user-manager (but with the arguments tweaked accordingly, of course).
e.g.or alternativelyCode: Select all$util->changeMenu('/ip hotspot user'); $util->add( array( 'name' => 'ex', 'password' => 'ex', 'mac-address' => '01:23:45:67:89:AB', 'limit-uptime' => '1h' ) );
Code: Select all$client->sendSync( new RouterOS\Request( '/ip hotspot user add name=ex password=ex mac-address=01:23:45:67:89:AB limit-uptime=1h' ) );
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).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).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
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.
$util->changeMenu('/ip hotspot user')->enable('user1');
$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'
]));
$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');
Does the part up toCode: Select all<?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.