123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #!/bin/bash
-
- # Hosted Zone ID e.g. BJBK35SKMM9OE
- ZONEID="Z49JKEQC08KW8"
-
- # The CNAME you want to update e.g. hello.example.com
- RECORDSET="aws.monkeybox.org"
-
- # More advanced options below
- # The Time-To-Live of this recordset
- TTL=300
- # Change this if you want
- COMMENT="Auto updating @ `date`"
- # Change to AAAA if using an IPv6 address
- TYPE="AAAA"
-
- # Get the external IP address
- IP=`curl -ss http://ipv6.icanhazip.com/`
- # IPv6 Validation:
- WORD="[0-9A-Fa-f]\{1,4\}"
- # flat address, no compressed words
- FLAT="^${WORD}\(:${WORD}\)\{7\}$"
- # ::'s compressions excluding beginning and end edge cases
- COMP2="^\(${WORD}:\)\{1,1\}\(:${WORD}\)\{1,6\}$"
- COMP3="^\(${WORD}:\)\{1,2\}\(:${WORD}\)\{1,5\}$"
- COMP4="^\(${WORD}:\)\{1,3\}\(:${WORD}\)\{1,4\}$"
- COMP5="^\(${WORD}:\)\{1,4\}\(:${WORD}\)\{1,3\}$"
- COMP6="^\(${WORD}:\)\{1,5\}\(:${WORD}\)\{1,2\}$"
- COMP7="^\(${WORD}:\)\{1,6\}\(:${WORD}\)\{1,1\}$"
- # trailing :: edge case, includes case of only :: (all 0's)
- EDGE_TAIL="^\(\(${WORD}:\)\{1,7\}\|:\):$"
- # leading :: edge case
- EDGE_LEAD="^:\(:${WORD}\)\{1,7\}$"
- function valid_ip()
- {
- # Returns 0 on a good IP, 1 on a bad IP
- local ip=$1
- echo $ip | grep --silent "\(${FLAT}\)\|\(${COMP2}\)\|\(${COMP3}\)\|\(${COMP4}\)\|\(${COMP5}\)\|\(${COMP6}\)\|\(${COMP7}\)\|\(${EDGE_TAIL}\)\|\(${EDGE_LEAD}\)"
- if [ $? -eq 0 ]; then
- return 0
- fi
- return 1
- }
-
- # Get current dir (stolen from http://stackoverflow.com/a/246128/920350)
- DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
- LOGFILE="$DIR/update-route53.ipv6.log"
- IPFILE="$DIR/update-route53.ipv6.ip"
-
- if ! valid_ip $IP; then
- echo "Invalid IP address: $IP" >> "$LOGFILE"
- exit 1
- fi
-
- # Check if the IP has changed
- if [ ! -f "$IPFILE" ]
- then
- touch "$IPFILE"
- fi
-
- if grep -Fxq "$IP" "$IPFILE"; then
- # code if found
- echo "IP is still $IP. Exiting" >> "$LOGFILE"
- exit 0
- else
- echo "IP has changed to $IP" >> "$LOGFILE"
- # Fill a temp file with valid JSON
- TMPFILE=$(mktemp /tmp/temporary-file.XXXXXXXX)
- cat > ${TMPFILE} << EOF
- {
- "Comment":"$COMMENT",
- "Changes":[
- {
- "Action":"UPSERT",
- "ResourceRecordSet":{
- "ResourceRecords":[
- {
- "Value":"$IP"
- }
- ],
- "Name":"$RECORDSET",
- "Type":"$TYPE",
- "TTL":$TTL
- }
- }
- ]
- }
- EOF
-
- # Update the Hosted Zone record
- aws route53 change-resource-record-sets \
- --hosted-zone-id $ZONEID \
- --change-batch file://"$TMPFILE" >> "$LOGFILE"
- echo "" >> "$LOGFILE"
-
- # Clean up
- rm $TMPFILE
- fi
-
- # All Done - cache the IP address for next time
- echo "$IP" > "$IPFILE"
|