ddns (885B)
1 #!/usr/bin/env bash 2 3 readonly HZ="${AWS_HOSTED_ZONE}" 4 readonly DOMAIN="${HOSTNAME}" 5 6 err() { 7 echo "[$(date -u +'%Y-%m-%dT%H:%M:%S%:z')]: $*" >&2 8 } 9 10 main() { 11 if ! [[ -x "$(command -v aws)" ]]; then 12 err "aws cli not installed"; exit 1 13 fi 14 15 # Get the IP address. 16 ip="$(dig -4 +short myip.opendns.com @resolver1.opendns.com)" 17 if [[ -z "${ip}" ]]; then 18 err "failed to get ip address"; exit 2 19 fi 20 printf "ip: %s\n" "${ip}" 21 22 # Update the domains. 23 update='{ 24 "Comment": "DDNS", 25 "Changes": [ 26 { 27 "Action": "UPSERT", 28 "ResourceRecordSet": { 29 "Name": "'"${DOMAIN}"'", 30 "Type": "A", 31 "TTL": 300, 32 "ResourceRecords": [{ "Value": "'"${ip}"'" }] 33 } 34 } 35 ] 36 }' 37 38 printf "requesting update for %s\n" "${DOMAIN}" 39 aws route53 change-resource-record-sets \ 40 --hosted-zone-id "${HZ}" \ 41 --change-batch "${update}" 42 } 43 44 main "$@"