Solved Get the DKIM key value for specific domain via API (CMD_API_DNS_CONTROL)

MaXi32

Verified User
Joined
Jul 25, 2016
Messages
645
Location
The Earth
Is there a good way to get DKIM value for a specific domain via API through curl in bash? Doesn't seems to have much documentation about this CMD_API_DNS_CONTROL: https://www.directadmin.com/features.php?id=626

Currently I use this method it works but I think this is too complex. I'm not sure if there is a simpler way than this:

Code:
#!/bin/bash
api_username="username"
api_password="12321"
da_port="2222"
hostname="test.hostname.com"
ssl="https"


command="CMD_API_DNS_CONTROL"
domain="test.com"
data="domain=${domain}"
method="POST"
input=$(curl --request "${method}" --user "${api_username}":"${api_password}" --data "${data}" "${ssl}://${hostname}:${da_port}/${command}")
decoded_status=$(printf '%b' "${input//%/\\x}") # Remove encoding character
echo "${decoded_status}" | grep 'x._domainkey' | awk '{ print $6" "$7" "$8}'

The sample output (the value of dkim key for domain test.com):

Code:
"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEXrHHDF4Yelpw2eAMFkQg69ZYCD09EBoJVlc4Zcgb0TNC+B0qNiO6X3UtXOgFiqlgU"

Is there a hidden feature that we able to query this easily using pure CMD_API_ ?
 
Last edited:
Got it, I forgot the ability to output json format. I can pass &json=yes and now it's easier to use jq to parse the data.

Full code for anyone interested using directadmin + curl + bash + jq:

Code:
#!/bin/bash
api_username="username"
api_password="12321"
da_port="2222"
hostname="test.hostname.com"
ssl="https"

command="CMD_API_DNS_CONTROL"
domain="test.com"
data="domain=${domain}&json=yes"
method="POST"

input=$(curl --silent --request "${method}" --user "${api_username}":"${api_password}" --data "${data}" "${ssl}://${hostname}:${da_port}/${command}" | jq -r ".records[] | select((.name=="x._domainkey") and (.type=="TXT")) | .value" | sed -e 's/^"//' -e 's/"$//')

echo "${input}"
This will display the correct DKIM for x._domainkey using jq. The earlier code wasn't correct because it didn't return full line of DKIM key.
 
Last edited:
Back
Top