I have the following code to add admin account via API using bash/curl. Code is working good but I'm not so sure how to get the return status:
if let say the username is incorrect, I can view this error using directadmin debug mode (running DA in debug mode). But how do you get the error code through curl request in this script?
I've tried adding return code $? as above but it just returns 0 (success) eventhough the request is not success. In PHP I saw example how to handle the error code but in bash I've not seen many example for this.
Bash:
#!/bin/bash
function cmd_api_account_admin() {
action="$1"
username="$2"
email="$3"
password="$4"
DA_ADMIN_USERNAME="admin"
DA_ADMIN_USERPASS="password"
DA_HOSTNAME_FQDN="server.domain.com"
DA_PORT="2222"
data="action=${action}&add=submit&username=${username}&email=${email}&passwd=${password}&passwd2=${password}¬ify=yes"
curl --request POST --user "${DA_ADMIN_USERNAME}":"${DA_ADMIN_USERPASS}" --data "${data}" "https://${DA_HOSTNAME_FQDN}:${DA_PORT}/CMD_API_ACCOUNT_ADMIN"
}
cmd_api_account_admin "create" "admin2" "[email protected]" "123456"
Bash:
curl --request POST --user "${DA_ADMIN_USERNAME}":"${DA_ADMIN_USERPASS}" --data "${data}"
retval=$?
echo "$retval"
I've tried adding return code $? as above but it just returns 0 (success) eventhough the request is not success. In PHP I saw example how to handle the error code but in bash I've not seen many example for this.
Last edited: