How to Mass Create cPanel Accounts

Posted on by Misael Ramirez | Updated:
Reading Time: 4 minutes

cPanel is one of the top contenders when it comes to control panels for production servers. This software is not mandatory, but it makes a huge difference when interacting with a server, providing a graphical interface, simplifying complex tasks, and automating processes. One of its core functions is creating a hosting account to which the user will be confined, giving the master user the option of limiting its resource consumption and system permissions, among other functions.

Creating a cPanel Account

Creating a new cPanel account is a relatively simple process. First, on the WHM (Web Host Manager) home screen, navigate to the Account Functions menu and click Create a New Account. This action will execute the account creation script, requiring you to input your domain information. Then, once complete, click Create.

This process will become a hassle quickly when we are trying to create dozens of accounts at one time. This article will cover how to automate batch account creation in a few minutes.

Creating Mass Templated Accounts with Bash Scripts 

We’ll be using a simple bash script to create our templated accounts.

Requirements

  • SSH access and root privileges.
  • Basic knowledge of Linux's file system.
  • FTP access, or experience in interacting with text editors (Vim, Nano, etc.).
  • WHM 11+.

Script Creation

Before getting started, we have to set up the script. To do so, we need to run the following commands by copying and pasting the entire code block.

touch cpanel_mass_account.sh
chmod +x cpanel_mass_account.sh
tee -a cpanel_mass_account.sh << END
#! /bin/sh

#This script can be used to mass create templated cPanel accounts
#Requires a list of domains/accounts with one account per line
clear
echo -e "Please provide the path of the file containing the list of accounts, each listed with the following format:\n\ndomain.com username password \n\nNote: Only the domain name is mandatory, default values will be configured if no other information is specified.\n"
read path

#Loop to make sure file exists
while true; do
        if  [[ -f "$path" ]] ; then
            break
        else
            echo -e "File does not exists, please try again.\n"
            read path
        fi
done
echo -e "\n"

#To empty the log, if the file exists
if [[ -f "/root/execution_results.log" ]]; then
echo -n "" > /root/execution_results.log
fi

#Loop to create an account for each element in the list
count=1
sed -i  "/^ *$/d" $path #Remove empty lines from the file
lines=$(cat $path|wc -l)
while [[ $count -le $lines ]]
do
        data=$(cat $path|awk "NR==$count")
        data_array=($data)
        domain=$(echo ${data_array[0]})
        username=$(echo ${data_array[1]})
        password=$(echo ${data_array[2]})

        if [[ -z "$username" ]]; then
                u1=$(echo "$domain" |cut -c1-4|cut -d '.' -f1)
                username=$(echo "$u1$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 3 | head -n 1)") #To avoid errors with similar domains, using urandom to attach 3 random characters to the username
                echo -e "\nNo username, generating one: $username"
        fi

        (echo -e "- Domain: $domain\n"; whmapi1 createacct domain=$domain username=$username password=$password  --output=xml 2> /dev/null; echo -e "\n") >> /root/execution_results.log
        results=$(grep "result" /root/execution_results.log|tail -n1)
        reason=$(grep "reason" /root/execution_results.log |tail -n1)
        reason=$(echo $reason|cut -d '>' -f2|cut -d '<' -f1)
        if [[ "$results" == *"1"* ]]; then
                echo "- Account $username for domain $domain, has been created successfully."
        else
                echo -e "Failed, there was an error during the account creation for domain $domain. Reason $reason"
        fi
(( count++ ))
done

echo -e "\nA log of this operation was saved at /root/execution_results.log"
END

The output will be an .sh file named cpanel_mass_account.sh with execution permissions.

How to Use It

We need to create a list for the script to work. We can use any text editor available on our server to generate the file or upload it via FTP. The file needs to be in plain text format.

When inputting each piece of information, follow the domain.com username password pattern.

Only the domain name is strictly necessary. If you do not provide a username and password for a domain, the system will automatically generate the information. Our example file is called list.txt.

superdomain.com user1 L3U?HFmryNW%U 
anotheronebitesthe.dust anotheruser &8T{x>J
another_one.org

Once we have the list, execute the script.

[root ]# ./cpanel_mass_account.sh

After the execution, a message like the one below should appear, asking for a list of domains to create the accounts.

mass-create-cpanel-accounts-list-accounts

Input the file path to where the file list.txt is located and press Enter.

Please provide the path of the file containing the list of accounts, each listed with the following format:

domain.com username password
...

/home/temp/list.txt

A log file will generate. Below is the typical output.

- Account user1 for domain superdomain.com, has been created successfully.
- Account anotheruser for domain anotheronebitesthe.dust, has been created successfully.
- Account randomuser for domain another_one.org, has been created successfully.
A log of this operation was saved at /root/execution_results.log

The bash script and the web browser’s interface produce the same output format and should work on any cPanel server with WHM 11+.

Breaking Down The Script

Let's take a look at the script itself.

...
echo -e "Please provide the path of the file containing the list of accounts, each listed with the following format:\n\ndomain.com username password..."
read path
...

#Loop to create an account for each element in the list
count=1
...
while [[ $count -le $lines ]]
do
...
        (echo -e "- Domain: $domain\n"; whmapi1 createacct domain=$domain username=$username password=$password  --output=xml 2> /dev/null; echo -e "\n") >> /root/execution_results.log
...
(( count++ ))
done

There are two main sections in the script. The first is when the list file containing the accounts to be created is requested.

...
echo -e "Please provide the path of the file containing the list of accounts, each listed with the following format:\n\ndomain.com username password..."
read path
...

The second is a simple while loop that will grab the information from the list, save it in an array, and use it as input to the WHM API (whmapi1) to create the accounts. The API will manage the error logging and the script will forward the results to the log file.

#Loop to create an account for each element in the list
count=1
...
while [[ $count -le $lines ]]
do
...
        (echo -e "- Domain: $domain\n"; whmapi1 createacct domain=$domain username=$username password=$password  --output=xml 2> /dev/null; echo -e "\n") >> /root/execution_results.log
...
(( count++ ))
done

Conclusion

You should now be able to mass-create cPanel accounts from a list file, taking a time-consuming task and making it more manageable. If you have questions about your cPanel account or need help selecting an appropriate hosting plan for your needs, our Support team is here 24 hours a day, 7 days a week, 365 days a year to help you out.

About the Author: Misael Ramirez

I have a degree in mechatronics; the career suited me because I'm always trying new things. I have a wide range of interests, but mainly I love music, movies (old ones), and physics.

Have Some Questions?

Our Sales and Support teams are available 24 hours by phone or e-mail to assist.

1.800.580.4985
1.517.322.0434

Latest Articles

Cloudstack vs OpenStack: Which is Right For You?

Read Article

Cloning an Existing Virtual Machine with VMware

Read Article

Five Steps to Create a Robots.txt File for Your Website

Read Article

Premium Business Email Pricing FAQ

Read Article

Microsoft Exchange Server Security Update

Read Article