Dwebb
Verified User
- Joined
- Mar 13, 2023
- Messages
- 10
Article: https://cainhosting.com/community/knowledge-base/directadmin-api-bash-script-to-create-user-account/
Bash:
#!/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
Bash:
./create_user.sh [username] [password] [email] [domain] [mysqldatabase] [mysqluser] [db-password]