1. Home
  2. DirectAdmin
  3. DirectAdmin bash script to create user account & database.

DirectAdmin bash script to create user account & database.

da bash script

Here CainHosting has created a bash script to work with the DirectAdmin API to create user accounts, assign packages, assign IP addresses, and also create MySQL database, MySQL user, & MySQL passwords for accounts automatically.

If you don’t need to create the MySQL automatically, you can delete everything after the section # MySQL Database details

Parts you need to change:

# Constants

  1. DIRECTADMIN_HOSTNAME
  2. DIRECTADMIN_USERNAME
  3. DIRECTADMIN_PASSWORD

# Create the user account

  1. package=Package_Name
  2. ip=IP_A.D.D.R.E.S.S

The Bash Script

#!/bin/bash

# Constants
DIRECTADMIN_HOSTNAME="your DA url"
DIRECTADMIN_PORT=2222
DIRECTADMIN_USERNAME="your DA admin username"
DIRECTADMIN_PASSWORD="your DA admin password"
DIRECTADMIN_HTTPS=true  # Set this to true if your DirectAdmin server uses HTTPS

# Command line arguments
USERNAME="$1"
PASSWORD="$2"
EMAIL="$3"
DOMAIN="$4"

# DirectAdmin API URL
if [ "$DIRECTADMIN_HTTPS" = true ]; then
  PROTOCOL="https"
else
  PROTOCOL="http"
fi
API_URL="${PROTOCOL}://${DIRECTADMIN_HOSTNAME}:${DIRECTADMIN_PORT}/CMD_API_ACCOUNT_USER"

# Create the user account
RESPONSE=$(curl -s -u "${DIRECTADMIN_USERNAME}:${DIRECTADMIN_PASSWORD}" -d "action=create&add=Submit&username=${USERNAME}&email=${EMAIL}&passwd=${PASSWORD}&passwd2=${PASSWORD}&domain=${DOMAIN}&package=Package_Name&ip=IP_A.D.D.R.E.S.S¬ify=no" "${API_URL}")

if [[ $RESPONSE == *"error=0"* ]]; then
  echo "User account created successfully."
else
  echo "Error creating user account: $RESPONSE"
fi

# MySQL Database details
DB_NAME="$5"
DB_USER="$6"
DB_PASSWORD="$7"

# DirectAdmin API URL for MySQL database creation
API_URL_DB="${PROTOCOL}://${DIRECTADMIN_HOSTNAME}:${DIRECTADMIN_PORT}/CMD_API_DATABASES"

# Create the MySQL database
RESPONSE_DB=$(curl -s -u "${DIRECTADMIN_USERNAME}|${USERNAME}:${DIRECTADMIN_PASSWORD}" -d "action=create&name=${DB_NAME}&user=${DB_USER}&passwd=${DB_PASSWORD}&passwd2=${DB_PASSWORD}" "${API_URL_DB}")

if [[ $RESPONSE_DB == *"error=0"* ]]; then
  echo "Database created successfully."
else
  echo "Error creating database. $RESPONSE_DB"
fi

How to use the script:

When you input the database name and database username, don’t include username_, this will be populated by the DirectAdmin API.

Example usage:

./create_user.sh [username] [password] [email] [domain] [mysqldatabase] [mysqluser] [db-password]

How the bash script works.

Before running the script edit # Constants changing the server’s hostname, port number, your DirectAdmin username and password, and whether the server uses HTTPS.

Run the script as command line arguments.

Sends a request to the DirectAdmin API using the provided info to create the user account, including the username, email, password, domain, package name, IP address, and notification preferences.

The script captures the API response.

The script also allows creation of a MySQL database by providing the name, username, and password.

Sends another request to the DirectAdmin API to create the database with provided information.

DirectAdmin: All About API

DirectAdmin has a feature-set which allows you to control almost everything you can via a browser, but using socket based scripts instead. This is called an “API”.

There are hundreds of possible API commands. Essentially, anything you can do normally in a browser, you can also do with the API. Just swap CMD_name to CMD_API_name (replace “name” with whatever the browser had), and use the same variables as the browser does running directadmin in debug mode ). Search for that CMD_API_name value in the Versions System

for more information.

Starting point and main API documentation:
http://www.directadmin.com/api.html

Php communication class:
http://forum.directadmin.com/showthread.php?t=258Some API command are listed in the api.html page above, but most only exist in the versions system:
http://www.directadmin.com/search_versions.php?query=CMD_APIMore examples:
http://www.directadmin.com/sample_api.txt
http://files.directadmin.com/services/all/httpsocket/examples/

Updated on June 26, 2023

Was this article helpful?

Related Articles

Add A Comment