diff --git a/scripts/ddns b/scripts/ddns new file mode 100755 index 0000000..eac715f --- /dev/null +++ b/scripts/ddns @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +readonly HZ="${AWS_HOSTED_ZONE}" +readonly DOMAIN="${HOSTNAME}" + +err() { + echo "[$(date -u +'%Y-%m-%dT%H:%M:%S%:z')]: $*" >&2 +} + +main() { + if ! [[ -x "$(command -v aws)" ]]; then + err "aws cli not installed"; exit 1 + fi + + # Get the IP address. + ip="$(dig -4 +short myip.opendns.com @resolver1.opendns.com)" + if [[ -z "${ip}" ]]; then + err "failed to get ip address"; exit 2 + fi + printf "ip: %s\n" "${ip}" + + # Update the domains. + update='{ + "Comment": "DDNS", + "Changes": [ + { + "Action": "UPSERT", + "ResourceRecordSet": { + "Name": "'"${DOMAIN}"'", + "Type": "A", + "TTL": 300, + "ResourceRecords": [{ "Value": "'"${ip}"'" }] + } + } + ] +}' + + printf "requesting update for %s\n" "${DOMAIN}" + aws route53 change-resource-record-sets \ + --hosted-zone-id "${HZ}" \ + --change-batch "${update}" +} + +main "$@" \ No newline at end of file