-
Notifications
You must be signed in to change notification settings - Fork 2
/
update-route53.sh
67 lines (60 loc) · 1.68 KB
/
update-route53.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
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
}
while [[ 1 ]]; do
CURRENT_IP=$(curl -sS $IP_PROVIDER)
LAST_IP=$(getent hosts $AWS_ROUTE53_HOST | awk '{ print $1 ; exit }')
if ! valid_ip $CURRENT_IP; then
echo "Invalid IP address CURRENT_IP: $CURRENT_IP"
sleep $UPDATE_INTERVAL
continue
fi
if [ $CURRENT_IP == $LAST_IP ]; then
echo "IP is the same ($CURRENT_IP)"
sleep $UPDATE_INTERVAL
continue
else
echo "IP has changed to $CURRENT_IP (last was $LAST_IP)"
# Fill a temp file with valid JSON
TMPFILE=$(mktemp /tmp/temporary-file.XXXXXXXX)
cat > ${TMPFILE} << EOF
{
"Comment":"Auto update @ `date`",
"Changes":[
{
"Action":"UPSERT",
"ResourceRecordSet":{
"ResourceRecords":[
{
"Value":"$CURRENT_IP"
}
],
"Name":"$AWS_ROUTE53_HOST",
"Type":"A",
"TTL":$AWS_ROUTE53_TTL
}
}
]
}
EOF
# Update the Hosted Zone record
echo "Updating Route53..."
aws route53 change-resource-record-sets \
--hosted-zone-id $AWS_ROUTE53_ZONEID \
--change-batch file://"$TMPFILE"
rm -f $TMPFILE
fi
sleep $UPDATE_INTERVAL
done