diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.txt | 6 | ||||
-rwxr-xr-x | update.sh | 40 |
3 files changed, 46 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..04204c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config @@ -4,4 +4,8 @@ Sometimes your servers IP changes underneath you. This util is script you can se == SETUP -Ensure your API keys are somewhere secure and accessible to the script +Ensure your API keys are somewhere secure and accessible to the script. It will be sourced by the script so should be sh env compatible (aka KEY=VALUE) + +== REQUIREMENTS + +curl, dig, jq diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..9fba7be --- /dev/null +++ b/update.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env sh +set -e +if [ $# -ne 2 ]; then + echo "Usage: ./update.sh DOMAIN API_CONFIG" >&2 + exit 1 +fi + +DOMAIN=$1 +API_CONFIG=$2 + +source $API_CONFIG +config_ip=$(dig +short $DOMAIN) +actual_ip=$(curl https://ipinfo.io/ip) + +if [ "$config_ip" != "$actual_ip" ]; then + echo "Updating IP from $config_id to $actual_ip for $DOMAIN" + res=$(curl -X POST "https://porkbun.com/api/json/v3/dns/retrieve/$DOMAIN" \ + -d "{ \"secretapikey\": \"${SECRETAPIKEY}\", \"apikey\": \"${APIKEY}\" }") + status=$(echo "$res" | jq -r '.status') + if [ "$status" != "SUCCESS" ]; then + echo "Failed to fetch domain ID:\n\t$res" >&2 + exit 1 + fi + ID=$(echo "$res" | jq -r ".records[] | select(.type == \"A\") | select(.content == \"${config_ip}\") | .id") + res=$(curl -X POST "https://porkbun.com/api/json/v3/dns/edit/$DOMAIN/$ID" \ + -d "{ \"secretapikey\": \"${SECRETAPIKEY}\", \"apikey\": \"${APIKEY}\", \"content\": \"${actual_ip}\", \"ttl\": \"600\", \"type\": \"A\", \"name\": \"\" }") + status=$(echo "$res" | jq -r '.status') + if [ "$status" == "SUCCESS" ]; then + echo "Successfully updated $DOMAIN record $ID from $config_ip to $actual_ip" + else + echo "Failed to update DNS record for $DOMAIN with $ID from $config_ip to $actual_ip" >&2 + pretty=$(echo "$res" | jq '.') + echo "$pretty" >&2 + exit 1 + fi +else + echo "$DOMAIN is currently registered properly: $config_ip $actual_ip" +fi + +exit 0 |