Skip to content

Instantly share code, notes, and snippets.

@mandusm
Created April 5, 2016 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandusm/f0c2a82a071309a3bddab146b797d932 to your computer and use it in GitHub Desktop.
Save mandusm/f0c2a82a071309a3bddab146b797d932 to your computer and use it in GitHub Desktop.
DynamicDNS Route53 Update.
#!/bin/bash
ZONEID=YOUR_HOSTED_ZONE_ID
export PATH=/bin:/usr/bin:/usr/local/bin
DNS_RECORD="$1"
function digIP () {
while true; do
CURRENT_IP=`dig +short +time=5 "$1"`
if [ -n "$CURRENT_IP" ]; then
echo "$CURRENT_IP";
return
else
sleep 5
fi
done
}
function curlIP {
ENDPOINT[0]="https://api.ipify.org"
ENDPOINT[1]="http://ifconfig.co/"
ENDPOINT[2]="http://ifconfig.me/"
i=0
while true; do
CURLED_IP=`curl -s ${ENDPOINT[$i]} --connect-timeout 10 -4`
if [ -n "$CURLED_IP" ]; then
echo "$CURLED_IP";
return
else
i=$i+1
sleep 5
fi
done
}
CURRENT_IP=`digIP "${DNS_RECORD}"`
CURLED_IP=`curlIP`
if [ "$CURRENT_IP" == "$CURLED_IP" ]; then
echo "[`date`] A Record ${CURRENT_IP} and detected IP ${CURLED_IP} still matches"
exit 0
else
echo "[`date`] The Current Record of ${CURRENT_IP} does not match detected IP ${CURLED_IP}"
fi
JSON='{
"HostedZoneId": "",
"ChangeBatch": {
"Comment": "Update IP",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "'${DNS_RECORD}'",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "'${CURLED_IP}'"
}
]
}
}
]
}
}'
echo $JSON > /tmp/ipUpdate.json
while true; do
aws route53 change-resource-record-sets --hosted-zone-id ${ZONEID} --cli-input-json file:///tmp/ipUpdate.json &> /tmp/R53Update.log
if [ $? -eq 0 ]; then
echo "Route53 Update Successfull"
exit 0
else
echo "Route 53 Update not successful"
sleep 5
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment