Automatic creation of Wi-Fi users for Mikrotik hotspot

//Automatic creation of Wi-Fi users for Mikrotik hotspot

Automatic creation of Wi-Fi users for Mikrotik hotspot

Are you using Wi-Fi hotspot for guests, maybe you would like to give them a card with username and password that they will use for a limited period after they establish a connection? Here we will explain to you how by using only a Linux Terminal you can create a large number of Wi-Fi users in only few steps. Do not worry if the number is 200,300 or 500 users, you will not need a whole day for it.
Wi-Fi hotspot is a wireless network, which users can connect only by using a password. To reach the Internet on this network they need to sign in the system. One of many options for efficiently using and controlling the wireless network is with a Mikrotik router.
For access and configuration the Mikrotik router, you will need the WinBox software. To reach the Hotspot menu first you need to enter the IP menu and after you can start configuring.
Mikrotik Hotspot can manage the usage of the resources of the Wi-Fi per user with some limitations:

  • Elapsed time of one session
  • Time limit of the overall connection per user
  • Traffic limit which is allowed
  • Bandwidth of the connection

The creation of users with Mikrotik manually can take a lot of time if we are talking about a large number of users. To create them there is a far more efficient way by only using the Terminal from a Linux operative system with a simple bash command. In the text bellow, we will execute some scripts that will:

  1.  Generate passwords for the users;
  2. Create a text file which will include the user cards that are printed and given to the actual users;
  3. Create the users in the Mikrotik device.

Once the users are created in the Mikrotik they can be automatically turned off when they exceed their time limit. You will need a separate folder because you will create new files.
The first step is generating the passwords in pass.txt file (-n 100 shows how many passwords will be created according to the number of users that you want to create) :

apg -a 1 -M n -n 100 -m 9 -x 9 -E 0 > pass.txt

Generate the file WifiCards.txt that later can be saved as Word document that can be used for editing and printing the cards.

With the variable “i” we indicate the first user (if i=100, the user will start with number 100 untill 199 for 100 users). The next command will read the passwords from the file pass.txt and generate a textual line, which will insert them in the file WifiCards.txt. Here you need to enter an SSID and the network password in order the users know where exactly to connect.

i=100; while read line
echo "---------------------------------";
echo "Network name (SSID): ______________";
echo "Network password: *********";
echo; echo Username: user$i;
echo Password: $line; ((i=i+1));
done < pass.txt > WifiCards.txt

The number of users 100 (i=100 below), should be configured for the following user. The next step is generating a script that will create users in the Mikrotik device. Again, we use the file pass.txt and the output values in the file We assume that the IP address of the Mikrotik device is

while read line
echo ssh admin@ "'/ip hotspot user add name=user$i password=$line profile=default server=hotspot1 limit-uptime=6h'";
done < pass.txt >

Execute the script for creation of users on the device. For this to work automatically you will need to enter your public SSH key in the configuration on the Mikrotik for the user admin or use sshpass to add the password in the command line. If you use sshpass, the command ssh admin@, it will be something like sshpass –p {password} admin @

chmod +x

If the script is successful, all the users created will be included in the file WifiCards.txt in the following order,

Network name (SSID): ——————-
Network password: ***********
Username: ————–
Password: **************

You can check the users through the interface on the software WinBox, where you can also see the number of users that used the connection time, or see the status of what is left from the connection.If you have any problems with some of these commands feel free to contact us. The commands can differ if you are using bash or different Mikrotik device.

By |2017-02-27T15:52:39+00:00February 27th, 2017|Hotspot|Comments Off on Automatic creation of Wi-Fi users for Mikrotik hotspot

About the Author: