Created
July 5, 2017 15:55
-
-
Save magicdude4eva/d3af60bae0707612923b1e4ab83e10c4 to your computer and use it in GitHub Desktop.
dyn_dns_acme.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
#DynDNS.com Domain API | |
CUSTOMERNAME='PLEASE-SET' | |
USERNAME='PLEASE-SET' | |
PASSWORD='PLEASE-SET' | |
DYNAPIURL="https://api.dynect.net/" | |
ZONE="PLEASE-SET" | |
fulldomain="" | |
sessionstatus="" | |
authtoken="" | |
record_id="" | |
#REST_API | |
######## Public functions ##################### | |
#Usage: dns_myapi_add _acme-challenge.www.domain.com "Challenge-code" | |
dns_dynect_add() { | |
fulldomain=$1 | |
txtvalue=$2 | |
_debug fulldomain "$fulldomain" | |
_debug txtvalue "$txtvalue" | |
_info "Using dynect" | |
if ! get_token; then | |
return 1 | |
fi | |
if [ ! -z "$authtoken" ]; then | |
if ! add_record "$fulldomain" "$textvalue"; then | |
return 1 | |
fi | |
if ! publish_zone; then | |
return 1 | |
fi | |
else | |
return 1 | |
fi | |
return 0 | |
} | |
#Usage: fulldomain txtvalue | |
#Remove the txt record after validation. | |
dns_dynect_rm() { | |
authtoken="" | |
sessionstatus="" | |
record_id="" | |
fulldomain=$1 | |
_debug fulldomain "$fulldomain" | |
_info "Using dynect" | |
if ! get_token; then | |
return 1 | |
fi | |
if [ ! -z "$authtoken" ]; then | |
if ! get_record_id; then | |
return 1 | |
fi | |
if ! rm_record "$fulldomain"; then | |
return 1 | |
fi | |
if ! publish_zone; then | |
return 1 | |
fi | |
else | |
return 1 | |
fi | |
return 0 | |
} | |
#################### Private functions below ################################## | |
#get Auth-Token | |
get_token() { | |
_info "Getting token" | |
data="{\"customer_name\":\"$CUSTOMERNAME\", \"user_name\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" | |
url="$DYNAPIURL""REST/Session/" | |
method="POST" | |
_debug data "$data" | |
_debug url "$url" | |
export _H1="Content-Type: application/json" | |
response="$(_post "$data" "$url" "" "$method")" | |
#response=$(curl --ipv4 -X POST -H "Content-Type: application/json" $url -d $data) | |
_debug response "$response" | |
sessionstatus=$(echo $response | grep -Po "(?<=\"status\": \")[^\"]*") | |
_debug sessionstatus "$sessionstatus" | |
if [ "$sessionstatus" = "success" ]; then | |
authtoken=$(echo $response | grep -Po "(?<=\"token\": \")[^\"]*") | |
_info "Token received" | |
_debug authtoken "$authtoken" | |
return 0 | |
fi | |
_err "get token failed" | |
return 1 | |
} | |
#add TXT record | |
add_record() { | |
_info "Adding TXT record" | |
data="{\"rdata\":{\"txtdata\":\"$txtvalue\"},\"ttl\":\"300\"}" | |
url="$DYNAPIURL""REST/TXTRecord/""$ZONE/""$fulldomain" | |
method="POST" | |
export _H1="Content-Type: application/json" | |
export _H2="Auth-Token: "$authtoken | |
response="$(_post "$data" "$url" "" "$method")" | |
sessionstatus=$(echo $response | grep -Po "(?<=\"status\": \")[^\"]*") | |
_debug response $response | |
_debug response $sessionstatus | |
if [ "$sessionstatus" = "success" ]; then | |
_info "TXT Record successfully added" | |
return 0 | |
fi | |
_err "add TXT record failed" | |
return 1 | |
} | |
#publish the zone | |
publish_zone() { | |
_info "Publishing zone" | |
data="{\"publish\":\"true\"}" | |
url="$DYNAPIURL""REST/Zone/""$ZONE/" | |
method="PUT" | |
export _H1="Content-Type: application/json" | |
export _H2="Auth-Token: "$authtoken | |
response="$(_post "$data" "$url" "" "$method")" | |
sessionstatus=$(echo $response | grep -Po "(?<=\"status\": \")[^\"]*") | |
if [ "$sessionstatus" = "success" ]; then | |
_info "Zone published" | |
return 0 | |
fi | |
_err "publish zone failed" | |
return 1 | |
} | |
#get record_id of TXT record so we can delete the record | |
get_record_id() { | |
_info "Getting record_id of TXT record" | |
url="$DYNAPIURL""REST/TXTRecord/""$ZONE/""$fulldomain" | |
export _H1="Content-Type: application/json" | |
export _H2="Auth-Token: $authtoken" | |
response="$(_get "$url" "" "")" | |
_debug response "$response" | |
sessionstatus=$(echo $response | grep -Po "(?<=\"status\": \")[^\"]*") | |
_debug sessionstatus "$sessionstatus" | |
if [ "$sessionstatus" = "success" ]; then | |
record_id=$(echo $response | grep -Po "(?<=\"data\": \[\"/REST/TXTRecord/$ZONE/$fulldomain/)[^\"]*") | |
_debug record_id "$record_id" | |
return 0 | |
fi | |
_err "getting record_id failed" | |
return 1 | |
} | |
#delete TXT record | |
rm_record() { | |
_info "Deleting TXT record" | |
url="$DYNAPIURL""REST/TXTRecord/""$ZONE/""$fulldomain/""$record_id/" | |
method="DELETE" | |
_debug url "$url" | |
export _H1="Content-Type: application/json" | |
export _H2="Auth-Token: $authtoken" | |
response="$(_post "" "$url" "" "$method")" | |
_debug response "$response" | |
sessionstatus=$(echo $response | grep -Po "(?<=\"status\": \")[^\"]*") | |
_debug sessionstatus "$sessionstatus" | |
if [ "$sessionstatus" = "success" ]; then | |
_info "TXT record successfully deleted" | |
return 0 | |
fi | |
_err "delete TXT record failed" | |
return 1 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Donations are always welcome
🍺 Please support me: If the above helped you in any way, then follow me on Twitter or send me some coins:
Go to Curve.com to add your Crypto.com card to ApplePay and signup to Crypto.com for a staking and free Crypto debit card.
Use Binance Exchange to trade #altcoins. Sign up with Coinbase and instantly get $10 in BTC. I also accept old-school PayPal.
If you have no crypto, follow me at least on Twitter.