Skip to content

Instantly share code, notes, and snippets.

@ddepaoli3
Forked from phybros/update-route53.sh
Last active March 23, 2022 07:01
Show Gist options
  • Save ddepaoli3/6bde8d7d5099f6e83f2d04c8dcc8ba9d to your computer and use it in GitHub Desktop.
Save ddepaoli3/6bde8d7d5099f6e83f2d04c8dcc8ba9d to your computer and use it in GitHub Desktop.
BASH Script to keep Route53 updated with your current external IP address
#!/bin/bash
############
## README ##
############
#This script updates DNS with the ip of the machine from which the script is launch.
#The instance or who launch this script needs this permission to hosted zone:
#
#
# route53:ChangeResourceRecordSets
#
#The script need 2 arguments:
#1. hosted zone id
#2. record set
#wget -O- https://gist.githubusercontent.com/ddepaoli3/update-route53.sh | bash /dev/stdin ${HostedZoneID} name.${HostedZoneName}
#
#To use private IP instead public ip add a third argument not mandatory:
#3. private
#wget -O- https://gist.githubusercontent.com/ddepaoli3/update-route53.sh | bash /dev/stdin ${HostedZoneID} name.${HostedZoneName} private
# (optional) You might need to set your PATH variable at the top here
# depending on how you run this script
#PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Hosted Zone ID e.g. BJBK35SKMM9OE
ZONEID=$1
# The CNAME you want to update e.g. hello.example.com
RECORDSET=$2
# More advanced options below
# The Time-To-Live of this recordset
TTL=300
# Change this if you want
COMMENT="Auto updating @ `date` with this script: https://gist.github.com/ddepaoli3/6bde8d7d5099f6e83f2d04c8dcc8ba9d"
# Change to AAAA if using an IPv6 address
TYPE="A"
# Get the external IP address from OpenDNS (more reliable than other providers)
#IP=`dig +short myip.opendns.com @resolver1.opendns.com`
IP=`wget --quiet http://checkip.amazonaws.com -O- || curl --silent http://checkip.amazonaws.com`
if [[ $# == 3 && $3 == 'private' ]]
then
IP=`hostname --ip-address`
else
IP=`wget --quiet http://checkip.amazonaws.com -O- || curl --silent http://checkip.amazonaws.com`
fi
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
}
# Get current dir
# (from http://stackoverflow.com/a/246128/920350)
DIR="/tmp"
LOGFILE="$DIR/update-route53.log"
IPFILE="$DIR/update-route53.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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment