-
Notifications
You must be signed in to change notification settings - Fork 73
/
route53_ddns_update.sh
executable file
·100 lines (91 loc) · 3.26 KB
/
route53_ddns_update.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
#################################################
# Bash script to update Route53 dynamic DNS
#
# Assumes you already have cli53 (<https://github.com/barnybug/cli53>)
# installed and properly configured.
#
# Export ROUTE53_ZONE and ROUTE53_RR_NAME environment variables
# as your route53 Zone Id and Record Set name, respectively.
#
#
#################################################
# Copyright 2014 Jason Antman <[email protected]> <http://www.jasonantman.com>
# Free for any use provided that patches are submitted back to me.
#
# The latest version of this script can be found at:
# <https://github.com/jantman/misc-scripts/blob/master/route53_ddns_update.sh>
#
# CHANGELOG:
#
# * 2018-06-27 Jason Antman <[email protected]>
# - validate that new WAN_IP is not empty and looks like an IP address
# - cli53 replace instead of delete and create
#
# * 2018-06-13 Jason Antman <[email protected]>
# - update for new Go-based cli53
#
# * 2017-09-29 Jason Antman <[email protected]>
# - switch from whatismyip.jasonantman.com to api.ipify.org
#
# * 2015-06-15 Jason Antman <[email protected]>
# - get config from env vars instead of hard-coded
# - get OLD_WAN_IP from cli53 instead of local cache file
# - drop TTL to 60s
#
# * 2015-05-31 Jason Antman <[email protected]>
# - update for new whatismyip.jasonantman.com
#
# * 2015-05-20 Jason Antman <[email protected]>
# - fix bug in WAN_IP
# - add logging
#
# * 2014-12-26 Jason Antman <[email protected]>
# - initial script
#
#################################################
LOG_TAG=$(basename "$0")
log () {
logger -p local7.info -t $LOG_TAG "$1"
}
log_err () {
logger -p local7.notice -t $LOG_TAG "$1"
}
if [ -z ${ROUTE53_ZONE+x} ]
then
>&2 echo "${LOG_TAG} - ERROR - ROUTE53_ZONE environment variable not set"
log_err "ERROR - ROUTE53_ZONE environment variable not set"
exit 1
fi
if [ -z ${ROUTE53_RR_NAME+x} ]
then
>&2 echo "${LOG_TAG} - ERROR - ROUTE53_RR_NAME environment variable not set"
log_err "ERROR - ROUTE53_RR_NAME environment variable not set"
exit 1
fi
log "Running with ZONE=${ROUTE53_ZONE} RR=${ROUTE53_RR_NAME}"
# get WAN IP and trim whitespace
WAN_IP=$(wget -q -O - --no-check-certificate https://api.ipify.org/ | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [[ -z "$WAN_IP" ]]; then
log_err "ERROR - WAN IP from https://api.ipify.org/ is empty! Failing."
exit 1
fi
if [[ ! "$WAN_IP" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
log_err "ERROR - WAN IP from https://api.ipify.org/ does not look like an IP address: $WAN_IP"
exit 1
fi
log "Found current WAN IP as ${WAN_IP}"
# Get your old WAN IP
OLD_WAN_IP=$(cli53 export $ROUTE53_ZONE | grep "^${ROUTE53_RR_NAME}[[:space:]]" | awk '{print $5}')
log "Found old WAN IP as ${OLD_WAN_IP}"
# See if the new IP is the same as the old IP.
if [ "$WAN_IP" = "$OLD_WAN_IP" ]; then
echo "IP Unchanged"
log "IP is unchanged - exiting"
# Don't do anything if th eIP didn't change
else
# The IP changed
log "Deleting current A record"
set -o pipefail
cli53 rrcreate --replace $ROUTE53_ZONE "$ROUTE53_RR_NAME 60 A $WAN_IP" 2>&1 | logger -p local7.info -t "${LOG_TAG}-rrcreate" && echo $WAN_IP > /var/CURRENT_WAN_IP.txt || logger -p local7.notice -t $LOG_TAG "cli53 rrcreate FAILED."
fi