Last active
August 17, 2020 22:06
-
-
Save jeffrade/6522c5c1ee3d9eb8d4656d57db5613d2 to your computer and use it in GitHub Desktop.
Bash script that checks for IP change and updates Route53
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/bash | |
# Usage: Launch in the background and pass RECORD_SET_NAME (e.g. sub.example.com.) | |
# as a first argument for the Route53 record. Log file can be found at /var/tmp/r53-record.log | |
NOTIFY_DIR=/var/tmp | |
LOG_FILE=${NOTIFY_DIR}/r53-record.log | |
CURR_IP_FILE=${NOTIFY_DIR}/ip.out | |
TMP_IP_FILE=${NOTIFY_DIR}/ip.tmp | |
IP_LKP_URL=https://ifconfig.me/ip | |
RECORD_SET_FILE=${NOTIFY_DIR}/r53-record-set.json | |
RECORD_SET_NAME=${1} | |
RECORD_SET_TTL=300 | |
function create_record_json { | |
cat << EOF > ${RECORD_SET_FILE} | |
{ | |
"Changes": [ | |
{ | |
"Action": "UPSERT", | |
"ResourceRecordSet": { | |
"Name": "${RECORD_SET_NAME}", | |
"Type": "A", | |
"TTL": ${RECORD_SET_TTL}, | |
"ResourceRecords": [ | |
{ | |
"Value": "${1}" | |
} | |
] | |
} | |
} | |
] | |
} | |
EOF | |
} | |
function update_route53_record { | |
local new_ip=$(cat ${CURR_IP_FILE}) | |
create_record_json "${new_ip}" | |
local resp=$(aws route53 change-resource-record-sets --hosted-zone-id ${HOSTED_ZONE_ID} --change-batch file://${RECORD_SET_FILE}) | |
echo "Updated route53 record:" >> ${LOG_FILE} | |
echo "${resp}" >> ${LOG_FILE} | |
} | |
create_record_json "127.0.0.1" | |
curl -sf $IP_LKP_URL > $CURR_IP_FILE | |
while [[ true ]]; do | |
printf $(curl -sf $IP_LKP_URL) > $TMP_IP_FILE | |
IP_DIFF=$(diff -w $TMP_IP_FILE $CURR_IP_FILE) | |
if [[ "$IP_DIFF" != "" ]]; then | |
echo "We have a new IP: $(cat $TMP_IP_FILE)" >> ${LOG_FILE} | |
cat $TMP_IP_FILE > $CURR_IP_FILE | |
update_route53_record | |
fi | |
sleep 300s | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script now has a home at https://github.com/jeffrade/proxy-server