Last active
December 15, 2020 01:38
-
-
Save jtmitchell/b1f4709509c2ea76407b to your computer and use it in GitHub Desktop.
Dynamic DNS script for Amazon Route53
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
| #!/bin/bash | |
| # Update Dynamic DNS | |
| CURRENTIP="" | |
| IPADDR="" | |
| # Various get IP websites | |
| # WWW_IP=http://www.focb.co.nz/myip.php | |
| WWW_IP=http://icanhazip.com/ | |
| # Route53 settings, AWS keys are in boto config | |
| ROUTE53_HOST_LIST="nutmeg.maungawhau.net.nz. maungawhau.net.nz." # notice trailing dot | |
| ROUTE53_ZONEID="xxxxxx" # Zone ID here | |
| ROUTE53=/usr/local/bin/route53 | |
| IPFILE=/var/tmp/dynamic-dns.ip | |
| [ -f $IPFILE ] && CURRENTIP=$(cat $IPFILE) | |
| IPADDR=$(curl -s $WWW_IP) | |
| [ -z "$IPADDR" ] && IPADDR=$PPP_LOCAL | |
| # abort if the IP is localhost - probably right after a reboot? | |
| [ "$IPADDR" = "127.0.0.1" ] && exit | |
| if [ ! "$IPADDR" = "$CURRENTIP" ]; then | |
| logger -t "Dynamic DNS" "Updating DNS to new IP ($IPADDR)" | |
| # Update AWS Route53 | |
| for ROUTE53_HOST in $ROUTE53_HOST_LIST; | |
| do | |
| ROUTE53_IP=$($ROUTE53 get $ROUTE53_ZONEID | awk '/^'${ROUTE53_HOST}'/{print $4}' | tr -d '\n') | |
| if [ -z "$ROUTE53_IP" ]; then | |
| # no record for this hostname - better make one | |
| logger -t "Dynamic DNS" "Creating new Route53 record $ROUTE53_HOST $IPADDR" | |
| $ROUTE53 add_record $ROUTE53_ZONEID $ROUTE53_HOST A $IPADDR | |
| elif [ "$ROUTE53_IP" != "$IPADDR" ]; then | |
| # update the record | |
| logger -t "Dynamic DNS" "Updating Route53 record for $ROUTE53_HOST from $ROUTE53_IP to $IPADDR" | |
| $ROUTE53 change_record $ROUTE53_ZONEID $ROUTE53_HOST A $IPADDR | |
| else | |
| logger -t "Dynamic DNS" "Route53 IP unchanged ($IPADDR)" | |
| fi | |
| echo >$IPFILE $IPADDR | |
| done | |
| else | |
| logger -t "Dynamic DNS" "External IP unchanged ($IPADDR)" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment