update-route53.ipv6.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. # Hosted Zone ID e.g. BJBK35SKMM9OE
  3. ZONEID="Z49JKEQC08KW8"
  4. # The CNAME you want to update e.g. hello.example.com
  5. RECORDSET="aws.monkeybox.org"
  6. # More advanced options below
  7. # The Time-To-Live of this recordset
  8. TTL=300
  9. # Change this if you want
  10. COMMENT="Auto updating @ `date`"
  11. # Change to AAAA if using an IPv6 address
  12. TYPE="AAAA"
  13. # Get the external IP address
  14. IP=`curl -ss http://ipv6.icanhazip.com/`
  15. # IPv6 Validation:
  16. WORD="[0-9A-Fa-f]\{1,4\}"
  17. # flat address, no compressed words
  18. FLAT="^${WORD}\(:${WORD}\)\{7\}$"
  19. # ::'s compressions excluding beginning and end edge cases
  20. COMP2="^\(${WORD}:\)\{1,1\}\(:${WORD}\)\{1,6\}$"
  21. COMP3="^\(${WORD}:\)\{1,2\}\(:${WORD}\)\{1,5\}$"
  22. COMP4="^\(${WORD}:\)\{1,3\}\(:${WORD}\)\{1,4\}$"
  23. COMP5="^\(${WORD}:\)\{1,4\}\(:${WORD}\)\{1,3\}$"
  24. COMP6="^\(${WORD}:\)\{1,5\}\(:${WORD}\)\{1,2\}$"
  25. COMP7="^\(${WORD}:\)\{1,6\}\(:${WORD}\)\{1,1\}$"
  26. # trailing :: edge case, includes case of only :: (all 0's)
  27. EDGE_TAIL="^\(\(${WORD}:\)\{1,7\}\|:\):$"
  28. # leading :: edge case
  29. EDGE_LEAD="^:\(:${WORD}\)\{1,7\}$"
  30. function valid_ip()
  31. {
  32. # Returns 0 on a good IP, 1 on a bad IP
  33. local ip=$1
  34. echo $ip | grep --silent "\(${FLAT}\)\|\(${COMP2}\)\|\(${COMP3}\)\|\(${COMP4}\)\|\(${COMP5}\)\|\(${COMP6}\)\|\(${COMP7}\)\|\(${EDGE_TAIL}\)\|\(${EDGE_LEAD}\)"
  35. if [ $? -eq 0 ]; then
  36. return 0
  37. fi
  38. return 1
  39. }
  40. # Get current dir (stolen from http://stackoverflow.com/a/246128/920350)
  41. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  42. LOGFILE="$DIR/update-route53.ipv6.log"
  43. IPFILE="$DIR/update-route53.ipv6.ip"
  44. if ! valid_ip $IP; then
  45. echo "Invalid IP address: $IP" >> "$LOGFILE"
  46. exit 1
  47. fi
  48. # Check if the IP has changed
  49. if [ ! -f "$IPFILE" ]
  50. then
  51. touch "$IPFILE"
  52. fi
  53. if grep -Fxq "$IP" "$IPFILE"; then
  54. # code if found
  55. echo "IP is still $IP. Exiting" >> "$LOGFILE"
  56. exit 0
  57. else
  58. echo "IP has changed to $IP" >> "$LOGFILE"
  59. # Fill a temp file with valid JSON
  60. TMPFILE=$(mktemp /tmp/temporary-file.XXXXXXXX)
  61. cat > ${TMPFILE} << EOF
  62. {
  63. "Comment":"$COMMENT",
  64. "Changes":[
  65. {
  66. "Action":"UPSERT",
  67. "ResourceRecordSet":{
  68. "ResourceRecords":[
  69. {
  70. "Value":"$IP"
  71. }
  72. ],
  73. "Name":"$RECORDSET",
  74. "Type":"$TYPE",
  75. "TTL":$TTL
  76. }
  77. }
  78. ]
  79. }
  80. EOF
  81. # Update the Hosted Zone record
  82. aws route53 change-resource-record-sets \
  83. --hosted-zone-id $ZONEID \
  84. --change-batch file://"$TMPFILE" >> "$LOGFILE"
  85. echo "" >> "$LOGFILE"
  86. # Clean up
  87. rm $TMPFILE
  88. fi
  89. # All Done - cache the IP address for next time
  90. echo "$IP" > "$IPFILE"