#!/usr/bin/env bash # Need to add your API key below or set as env variable apikey="$HOSTWAY_API_KEY" # This script adds a token to dynu.com DNS for the ACME challenge # usage dns_add_dynu "domain name" "token" # return codes are; # 0 - success # 1 - error in input # 2 - error within internal processing # 3 - error in result ( domain not found in dynu.com etc) fulldomain="${1}" token="${2}" API='https://api.hostway.com/dns' # Check initial parameters if [[ -z "$fulldomain" ]]; then echo "DNS script requires full domain name as first parameter" exit 1 fi if [[ -z "$token" ]]; then echo "DNS script requires challenge token as second parameter" exit 1 fi curl_params=( -H "accept: application/json" -H "Authorization: Basic $apikey" -H 'Content-Type: application/json charset=utf-8') # Get domain id # curl -X GET "https://api.hostway.com/dns/domain/" resp=$(curl --silent "${curl_params[@]}" -X GET "$API/${fulldomain}") # Match domain id re="\"serial\":\s?([^}]*)" if [[ "$resp" =~ $re ]]; then domain_id="${BASH_REMATCH[1]}" fi if [[ -z "$domain_id" ]]; then echo 'Domain name not found on your Hostway account' exit 3 fi # Check for existing _acme-challenge TXT record # curl -X GET "https://api.hostway.com/dns/domain/records?filterType=TXT&page=1&pageSize=100" resp=$(curl --silent "${curl_params[@]}" -X GET "$API/${fulldomain}/records?filterType=TXT") #re="\"id\":\s?([^}]*)" re="(?<=_acme(.*)\"id\":\s?)[0-9]+(?=\})" if [[ "$resp" =~ $re ]]; then record_id="${BASH_REMATCH[1]}" fi if [[ -z "$record_id" ]]; then echo "Not able to find a record to delete" else # Delete existing record # curl -X DELETE https://api.hostway.com/dns/{domain}/records/{record_id} resp=$(curl --silent \ "${curl_params[@]}" \ -X DELETE "${API}/${fulldomain}/records/${record_id}") if [[ "$resp" == "" ]]; then echo "Record deleted successfully for ${fulldomain}" fi fi