Created
June 6, 2012 07:49
-
-
Save hSATAC/2880516 to your computer and use it in GitHub Desktop.
aws ec2 dynamic dns for route 53 (CNAME)
This file contains 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/sh | |
# Setup dynamic dns on Route 53 for aws ec2 (CNAME) | |
# | |
# Modified from Johan Lindh's script | |
# | |
# Script requirements: | |
# | |
# wget | |
# grep | |
# sed | |
# dig | |
# cut | |
# openssl | |
# base64 | |
# | |
# Most if not all of these come standard on *nix distros. | |
# | |
# The domain and host name to update | |
# and the desired TTL of the record | |
Domain=your.domain.net | |
Hostname=`hostname` | |
NewTTL=600 | |
# The Amazon Route 53 zone ID for the domain | |
# and the Amazon ID and SecretKey. Remember to | |
# ensure that this file can't be read by | |
# unauthorized people! | |
ZoneID=Z1234567890 | |
AmazonID=A1234567890 | |
SecretKey=GR$WYTJ%Y$@GY%J$%GY@H | |
# Enter the URL used to check extern IP | |
CheckIPURL='http://169.254.169.254/latest/meta-data/public-hostname' | |
# Enter some static text that immediately preceeds the current IP in the HTML output | |
# Note that you'll probably need to look at the actual HTML code to find this | |
CheckIPText='Current IP Address:' | |
############################################################### | |
# You should not need to change anything beyond this point | |
############################################################### | |
# Find an authoritative AWS R53 nameserver so we get a clean TTL | |
AuthServer=$(dig NS $Domain | grep -v ';' | grep -m 1 awsdns | grep $Domain | cut -f 6) | |
if [ "$AuthServer" = "" ]; then | |
echo The domain $Domain has no authoritative Amazon Route 53 name servers | |
exit 1 | |
fi | |
# Get the record and extract its parts | |
Record=$(dig @$AuthServer A $Hostname.$Domain | grep -v ";" | grep "$Hostname\.$Domain") | |
OldType=$( echo $Record | cut -d ' ' -f 4 ) | |
OldTTL=$( echo $Record | cut -d ' ' -f 2 ) | |
OldIP=$( echo $Record | cut -d ' ' -f 5 | sed s/.$//) | |
# Make sure it is an A record (could be CNAME) | |
if [ "$Record" != "" -a "$OldType" != "CNAME" ]; then | |
echo $Hostname.$Domain has a $OldType record, expected 'CNAME' | |
exit 1 | |
fi | |
# Retrieve the current IP | |
CurrentIP=$(wget "$CheckIPURL" -o /dev/null -O /dev/stdout) | |
# Changeset preamble | |
Changeset="" | |
Changeset=$Changeset"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
Changeset=$Changeset"<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2010-10-01/\">" | |
Changeset=$Changeset"<ChangeBatch><Comment>Update $Hostname.$Domain</Comment><Changes>" | |
if [ "$OldIP" != "" ]; then | |
# Add a DELETE request to the changeset | |
Changeset=$Changeset"<Change>" | |
Changeset=$Changeset"<Action>DELETE</Action>" | |
Changeset=$Changeset"<ResourceRecordSet>" | |
Changeset=$Changeset"<Name>$Hostname.$Domain.</Name>" | |
Changeset=$Changeset"<Type>CNAME</Type>" | |
Changeset=$Changeset"<TTL>$OldTTL</TTL>" | |
Changeset=$Changeset"<ResourceRecords>" | |
Changeset=$Changeset"<ResourceRecord>" | |
Changeset=$Changeset"<Value>$OldIP</Value>" | |
Changeset=$Changeset"</ResourceRecord>" | |
Changeset=$Changeset"</ResourceRecords>" | |
Changeset=$Changeset"</ResourceRecordSet>" | |
Changeset=$Changeset"</Change>" | |
fi | |
# Add CREATE request to the changeset | |
Changeset=$Changeset"<Change>" | |
Changeset=$Changeset"<Action>CREATE</Action>" | |
Changeset=$Changeset"<ResourceRecordSet>" | |
Changeset=$Changeset"<Name>$Hostname.$Domain.</Name>" | |
Changeset=$Changeset"<Type>CNAME</Type>" | |
Changeset=$Changeset"<TTL>$NewTTL</TTL>" | |
Changeset=$Changeset"<ResourceRecords>" | |
Changeset=$Changeset"<ResourceRecord>" | |
Changeset=$Changeset"<Value>$CurrentIP</Value>" | |
Changeset=$Changeset"</ResourceRecord>" | |
Changeset=$Changeset"</ResourceRecords>" | |
Changeset=$Changeset"</ResourceRecordSet>" | |
Changeset=$Changeset"</Change>" | |
# Close the changeset | |
Changeset=$Changeset"</Changes>" | |
Changeset=$Changeset"</ChangeBatch>" | |
Changeset=$Changeset"</ChangeResourceRecordSetsRequest>" | |
if [ "$OldIP" != "$CurrentIP" ]; then | |
# Get the date at the Amazon servers | |
CurrentDate=$(wget -q -S https://route53.amazonaws.com/date -O /dev/null 2>&1 | grep Date | sed 's/.*Date: //') | |
# Calculate the SHA1 hash and required headers | |
Signature=$(echo -n $CurrentDate | openssl dgst -binary -sha1 -hmac $SecretKey | base64) | |
DateHeader="Date: "$CurrentDate | |
AuthHeader="X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=$AmazonID,Algorithm=HmacSHA1,Signature=$Signature" | |
# Submit request | |
Result=$(wget -nv --header="$DateHeader" --header="$AuthHeader" --header="Content-Type: text/xml; charset=UTF-8" --post-data="$Changeset" -O /dev/stdout -o /dev/stdout https://route53.amazonaws.com/2010-10-01/hostedzone/$ZoneID/rrset) | |
if [ "$?" -ne "0" ]; then | |
echo "Failed to update $Hostname.$Domain: "$Result | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment