As previous articles have stated, one of my test environments is a direct admin installation with Softaculous installed and one of the issues I encounter is that I want my installations to be mainly on subdomains.
But unlike my Gridpane setup, I can’t generate a subdomain and add it to the account via Cloudflare, for example, automatically on installation. This has been on my list of things to fix and yesterday I finalised a simple BAT script which will use the DirectAdmin API to create subdomains and also remove subdomains from this test environment.
I can now load a cmd window on my Windows machine and type DA (this bat is pathed and called DA) and it will then give me the option of which domain to install the subdomain on in the account and then ask the name it will also output all subdomains (including the new one) and give me the option to delete all or one of them.
All options in this script except for domain selection can be bypassed by just pushing enter, so it can be used just to create or just to delete too, it’s quick and efficient, and by the time I’ve loaded DirectAdmin and Softaculous using my stream deck buttons, I am fully engaged, and the subdomain has been created.
@echo off
setlocal enabledelayedexpansion
:: ===========================================================
:: CONFIGURATION: Set your DirectAdmin credentials and server
:: ===========================================================
set "DA_USER=username"
set "DA_PASS=password"
set "DA_HOST=domains or ip"
set "DA_PORT=2222"
:: Clear previous debug log (if any)
if exist debug.log del debug.log
:: ===========================================================
:: STEP 1: Retrieve list of domains using HTTPS
:: ===========================================================
echo Fetching list of domains...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SHOW_DOMAINS" > "domains.txt"
if not exist "domains.txt" (
echo Failed to fetch domains.
goto :EOF
)
:: ===========================================================
:: STEP 2: Parse the domain list and display them
:: Expected format: list[]=domain1&list[]=domain2&...
:: ===========================================================
set "line="
set /p line=<"domains.txt"
if "%line%"=="" (
echo No domains were returned.
goto :EOF
)
set /A i=0
for %%A in (%line:&= %) do (
set "token=%%A"
set "domain=!token:list[]=!"
if not "!domain!"=="" (
set /A i+=1
set "DOMAIN[!i!]=!domain!"
echo !i!: !domain!
)
)
if %i%==0 (
echo No valid domains found.
goto :EOF
)
set /P choice="Select a domain by number: "
set "selected_domain=!DOMAIN[%choice%]!"
if "!selected_domain!"=="" (
echo Invalid selection.
goto :EOF
)
echo You selected: !selected_domain!
echo.
:: ===========================================================
:: STEP 3: Optionally add a new subdomain
:: ===========================================================
set /P subdomain="Enter the subdomain name to add (or press Enter to skip): "
if not "!subdomain!"=="" (
echo Checking if subdomain "!subdomain!.!selected_domain!" exists...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "domain=!selected_domain!" > "subdomains.txt"
(echo --- Retrieved subdomains --- >> debug.log
type "subdomains.txt" >> debug.log
echo --------------------------- >> debug.log)
echo Searching for "!subdomain!"...
echo !subdomain! | findstr /I /C:"!subdomain!" "subdomains.txt" >nul
if !errorlevel! equ 0 (
echo Subdomain "!subdomain!.!selected_domain!" already exists.
) else (
echo Creating subdomain "!subdomain!.!selected_domain!"...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "action=create&domain=!selected_domain!&subdomain=!subdomain!" > "create.tmp"
(echo --- Create command output --- >> debug.log
type "create.tmp" >> debug.log
echo ---------------------------- >> debug.log)
del "create.tmp"
echo Subdomain created.
)
echo.
)
:: ===========================================================
:: STEP 4: Retrieve the subdomain list for deletion
:: ===========================================================
echo Retrieving subdomains for !selected_domain!...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "domain=!selected_domain!" > "subdomains.txt"
echo --- Raw subdomain list ---
type "subdomains.txt"
echo.
:: ===========================================================
:: STEP 5: Parse the subdomain list into an array
:: Expected output: list[]=val1&list[]=val2&... (with possible trailing dashes)
:: ===========================================================
set "subLine="
set /p subLine=<"subdomains.txt"
:: Remove trailing dashes:
set "subLine=!subLine:---------------------------=!"
:: Replace & with a space:
set "subLine=!subLine:&= !"
>> debug.log echo Cleaned subLine: !subLine!
set /A j=0
for %%S in (!subLine!) do (
set "token=%%S"
set "token=!token:list[]=!"
if not "!token!"=="" (
set /A j+=1
set "SUB[!j!]=!token!"
echo !j!: !token!.!selected_domain!
)
)
if %j%==0 (
echo No subdomains found.
goto :DeletionDone
)
echo.
:: ===========================================================
:: STEP 6: Prompt for deletion choice
:: ===========================================================
echo Enter the number of the subdomain to delete, type 99 to delete ALL, or press Enter to skip deletion:
set /P delChoice="Your choice: "
if "!delChoice!"=="" (
echo No deletion requested.
goto :DeletionDone
)
if "!delChoice!"=="99" (
echo Deleting ALL subdomains for !selected_domain!...
for /L %%I in (1,1,!j!) do (
set "thisSub=!SUB[%%I]!"
if not "!thisSub!"=="" (
echo Deleting subdomain "!thisSub!.!selected_domain!"...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "action=delete&domain=!selected_domain!&select0=!thisSub!" > "delete.tmp"
>> debug.log echo Delete command output for !thisSub!:
type "delete.tmp" >> debug.log
del "delete.tmp"
)
)
echo All subdomains deletion requested.
) else (
:: Use CALL to get the chosen subdomain from the SUB array
call set "chosenSub=%%SUB[%delChoice%]%%"
if "!chosenSub!"=="" (
echo Invalid subdomain selection.
goto :DeletionDone
)
echo Deleting subdomain "!chosenSub!.!selected_domain!"...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "action=delete&domain=!selected_domain!&select0=!chosenSub!" > "delete.tmp"
>> debug.log echo Delete command output for !chosenSub!:
type "delete.tmp" >> debug.log
del "delete.tmp"
echo Subdomain "!chosenSub!.!selected_domain!" deletion requested.
)
:DeletionDone
:: ===========================================================
:: STEP 7: Re-check the subdomain list after deletion
:: ===========================================================
echo.
echo Re-fetching subdomains for !selected_domain!...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "domain=!selected_domain!" > "subdomains_after.txt"
echo --- Final subdomain list ---
type "subdomains_after.txt"
echo -----------------------------
del "subdomains_after.txt"
:: Clean up the debug log
if exist debug.log del debug.log
pause
BAT (Batchfile)Below is Version 2.0 which uses SSH to delete the directories created but not removed with the subdmain this assumes you have your SSH ky set as id_rsa for automatic command line login to allow passwordless deletion
@echo off
setlocal enabledelayedexpansion
:: ===========================================================
:: CONFIGURATION: Set your DirectAdmin credentials and server
:: ===========================================================
set "DA_USER=username"
set "DA_PASS=password"
set "DA_HOST=domains or ip"
set "DA_PORT=2222"
:: ===========================================================
:: SSH CONFIGURATION
:: ===========================================================
set "SSH_USER=sshuser"
set "SSH_HOST=host"
set "SSH_PORT=port"
:: Clear previous debug log (if any)
if exist debug.log del debug.log
:: ===========================================================
:: STEP 1: Retrieve list of domains using HTTPS
:: ===========================================================
echo Fetching list of domains...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SHOW_DOMAINS" > "domains.txt"
if not exist "domains.txt" (
echo Failed to fetch domains.
goto :EOF
)
:: ===========================================================
:: STEP 2: Parse the domain list and display them
:: Expected format: list[]=domain1&list[]=domain2&...
:: ===========================================================
set "line="
set /p line=<"domains.txt"
if "%line%"=="" (
echo No domains were returned.
goto :EOF
)
set /A i=0
for %%A in (%line:&= %) do (
set "token=%%A"
set "domain=!token:list[]=!"
if not "!domain!"=="" (
set /A i+=1
set "DOMAIN[!i!]=!domain!"
echo !i!: !domain!
)
)
if %i%==0 (
echo No valid domains found.
goto :EOF
)
set /P choice="Select a domain by number: "
set "selected_domain=!DOMAIN[%choice%]!"
if "!selected_domain!"=="" (
echo Invalid selection.
goto :EOF
)
echo You selected: !selected_domain!
echo.
:: ===========================================================
:: STEP 3: Optionally add a new subdomain
:: ===========================================================
set /P subdomain="Enter the subdomain name to add (or press Enter to skip): "
if not "!subdomain!"=="" (
echo Checking if subdomain "!subdomain!.!selected_domain!" exists...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "domain=!selected_domain!" > "subdomains.txt"
(echo --- Retrieved subdomains --- >> debug.log
type "subdomains.txt" >> debug.log
echo --------------------------- >> debug.log)
echo Searching for "!subdomain!"...
echo !subdomain! | findstr /I /C:"!subdomain!" "subdomains.txt" >nul
if !errorlevel! equ 0 (
echo Subdomain "!subdomain!.!selected_domain!" already exists.
) else (
echo Creating subdomain "!subdomain!.!selected_domain!"...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "action=create&domain=!selected_domain!&subdomain=!subdomain!" > "create.tmp"
(echo --- Create command output --- >> debug.log
type "create.tmp" >> debug.log
echo ---------------------------- >> debug.log)
del "create.tmp"
echo Subdomain created.
)
echo.
)
:: ===========================================================
:: STEP 4: Retrieve the subdomain list for deletion
:: ===========================================================
echo Retrieving subdomains for !selected_domain!...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "domain=!selected_domain!" > "subdomains.txt"
echo --- Raw subdomain list ---
type "subdomains.txt"
echo.
:: ===========================================================
:: STEP 5: Parse the subdomain list into an array
:: Expected output: list[]=val1&list[]=val2&... (with possible trailing dashes)
:: ===========================================================
set "subLine="
set /p subLine=<"subdomains.txt"
:: Remove trailing dashes:
set "subLine=!subLine:---------------------------=!"
:: Replace & with a space:
set "subLine=!subLine:&= !"
>> debug.log echo Cleaned subLine: !subLine!
set /A j=0
for %%S in (!subLine!) do (
set "token=%%S"
set "token=!token:list[]=!"
if not "!token!"=="" (
set /A j+=1
set "SUB[!j!]=!token!"
echo !j!: !token!.!selected_domain!
)
)
if %j%==0 (
echo No subdomains found.
goto :DeletionDone
)
echo.
:: ===========================================================
:: STEP 6: Prompt for deletion choice (WITH SSH FOLDER DELETION)
:: ===========================================================
echo Enter the number of the subdomain to delete, type 99 to delete ALL, or press Enter to skip deletion:
set /P delChoice="Your choice: "
if "!delChoice!"=="" (
echo No deletion requested.
goto :DeletionDone
)
if "!delChoice!"=="99" (
echo Deleting ALL subdomains for !selected_domain!...
for /L %%I in (1,1,!j!) do (
set "thisSub=!SUB[%%I]!"
if not "!thisSub!"=="" (
echo Deleting subdomain "!thisSub!.!selected_domain!"...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "action=delete&domain=!selected_domain!&select0=!thisSub!" > "delete.tmp"
>> debug.log echo Delete command output for !thisSub!:
type "delete.tmp" >> debug.log
del "delete.tmp"
:: SSH Folder Deletion
echo Removing folders via SSH for !thisSub!.!selected_domain!...
ssh -p %SSH_PORT% %SSH_USER%@%SSH_HOST% "rm -rf /home/%SSH_USER%/domains/!thisSub!.!selected_domain!"
ssh -p %SSH_PORT% %SSH_USER%@%SSH_HOST% "rm -rf /home/%SSH_USER%/domains/!selected_domain!/public_html/!thisSub!"
echo Folders removed via SSH.
)
)
echo All subdomains deletion requested.
) else (
:: Use CALL to get the chosen subdomain from the SUB array
call set "chosenSub=%%SUB[%delChoice%]%%"
if "!chosenSub!"=="" (
echo Invalid subdomain selection.
goto :DeletionDone
)
echo Deleting subdomain "!chosenSub!.!selected_domain!"...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "action=delete&domain=!selected_domain!&select0=!chosenSub!" > "delete.tmp"
>> debug.log echo Delete command output for !chosenSub!:
type "delete.tmp" >> debug.log
del "delete.tmp"
echo Subdomain "!chosenSub!.!selected_domain!" deletion requested.
:: SSH Folder Deletion
echo Removing folders via SSH for !chosenSub!.!selected_domain!...
ssh -p %SSH_PORT% %SSH_USER%@%SSH_HOST% "rm -rf /home/%SSH_USER%/domains/!chosenSub!.!selected_domain!"
ssh -p %SSH_PORT% %SSH_USER%@%SSH_HOST% "rm -rf /home/%SSH_USER%/domains/!selected_domain!/public_html/!chosenSub!"
echo Folders removed via SSH.
)
:DeletionDone
:: ===========================================================
:: STEP 7: Re-check the subdomain list after deletion
:: ===========================================================
echo.
echo Re-fetching subdomains for !selected_domain!...
curl --silent --insecure --user "%DA_USER%:%DA_PASS%" "https://%DA_HOST%:%DA_PORT%/CMD_API_SUBDOMAINS" --data "domain=!selected_domain!" > "subdomains_after.txt"
echo --- Final subdomain list ---
type "subdomains_after.txt"
echo -----------------------------
del "subdomains_after.txt"
:: Clean up the debug log
if exist debug.log del debug.log
pause
BAT (Batchfile)You can see a bubble of the script in action and how I use it below