Community discussions

MikroTik App
 
aboshhab
just joined
Topic Author
Posts: 17
Joined: Tue Nov 20, 2018 9:00 am

how can add 1000 hotspot users fast by php api

Fri Mar 25, 2022 1:40 am

hi I'm using API Php Class But I want to know why api is too slow in execute multiple commands?

for example i need to add 1000 new users to hotspot I'm using for loop in php, it's take long time.
how can i send them one command to mikrotik not one by one.

$API = new RouterosAPI();
if ($API->connect($mikrotik_ip, $username, $password )) {

for ($i=0; $i < $usernumber; $i++) {
$user = random_int(1111,9999);
$pass = random_int(111,999);
$i = $API->comm("/ip/hotspot/user/add", array("name"=>"$user","password"=>"$pass","limit-uptime"=>"24h"));

}
}


this is my class
https://github.com/BenMenking/routeros-api
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: how can add 1000 hotspot users fast by php api

Fri Mar 25, 2022 2:59 pm

On that script I see one big mistake: random called 1000 time on 9999 number interval can provide more time the same number.... it's RANDOM...
(Also called only 2 times can provide the same number)

Better is, for example start from user 1111 and add a random number between 1 and 8 for obtain the next number, and do it 1000 times, etc.
(in the worst case (all time 8 ) 1000 * 8 = 8000, 8000 + 1111 = 9111)


And about the function, is better to do that inside a called script on routerboard,
for example:
:global userStart 1111
:global stepStart 1
:global stepEnd 8
:global userNumber 1000
:global userMaxUptime 24h
/sys script run generate_hs_users
:
 
aboshhab
just joined
Topic Author
Posts: 17
Joined: Tue Nov 20, 2018 9:00 am

Re: how can add 1000 hotspot users fast by php api

Fri Mar 25, 2022 3:51 pm

On that script I see one big mistake: random called 1000 time on 9999 number interval can provide more time the same number.... it's RANDOM...
(Also called only 2 times can provide the same number)

Better is, for example start from user 1111 and add a random number between 1 and 8 for obtain the next number, and do it 1000 times, etc.
(in the worst case (all time 8 ) 1000 * 8 = 8000, 8000 + 1111 = 9111)


And about the function, is better to do that inside a called script on routerboard,
for example:
:global userStart 1111
:global stepStart 1
:global stepEnd 8
:global userNumber 1000
:global userMaxUptime 24h
/sys script run generate_hs_users
:
thank you alot
please complete the code
last step you mentioned, do you mean i write scripts in the system/script and call it my php api?
if that right how can use it to print them pdf
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: how can add 1000 hotspot users fast by php api

Fri Mar 25, 2022 3:58 pm

If you need to print on pdf, you need more complicate application,
is not sufficent to generate the script locally, and send it by ftp the file newusers.auto.rsc (more faster than 1000 api calls...)
Sorry, but I do not write the scripts for you.
I give only hints.

Who is online

Users browsing this forum: No registered users and 17 guests