1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #!/bin/bash
-
- # 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="A"
-
- # Get the external IP address
- IP=`curl -ss http://ipv4.icanhazip.com/`
-
- function valid_ip()
- {
- local ip=$1
- local stat=1
-
- if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
- OIFS=$IFS
- IFS='.'
- ip=($ip)
- IFS=$OIFS
- [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
- && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
- stat=$?
- fi
- return $stat
- }
- DIR=/scratch/
- LOGFILE="$DIR/update-route53.log"
- IPFILE="$DIR/update-route53.ip"
-
- if ! valid_ip $IP; then
- echo "Invalid IP address: $IP" | tee -a "$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" | tee -a "$LOGFILE"
- exit 0
- else
- echo "IP has changed to $IP" | tee -a "$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" | tee -a "$LOGFILE"
- echo "" | tee -a "$LOGFILE"
-
- # Clean up
- rm $TMPFILE
- fi
-
- # All Done - cache the IP address for next time
- echo "$IP" > "$IPFILE"
|