-
Notifications
You must be signed in to change notification settings - Fork 12
/
update_ipv6.sh
48 lines (41 loc) · 1.42 KB
/
update_ipv6.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
#!/bin/sh
#
# Updates a zone record using Gandi's LiveDNS.
# prevent shell to expand wildcard record
set -f
API="https://api.gandi.net/v5/livedns/"
IP_SERVICE="http://me.gandi.net"
if [[ -z "${GANDI_PAT}" && ! -z ${APIKEY} ]]; then
GANDI_PAT=${APIKEY}
fi
if [[ -z "${FORCE_IPV6}" ]]; then
WAN_IPV6=$(curl -s6 ${IP_SERVICE})
if [[ -z "${WAN_IPV6}" ]]; then
echo "$(date "+[%Y-%m-%d %H:%M:%S]") [ERROR] Something went wrong. Can not get your IPv6 from ${IP_SERVICE}"
exit 1
fi
else
WAN_IPV6="${FORCE_IPV6}"
fi
for RECORD in ${RECORD_LIST//;/ } ; do
if [ "${RECORD}" = "@" ] || [ "${RECORD}" = "*" ]; then
SUBDOMAIN="${DOMAIN}"
else
SUBDOMAIN="${RECORD}.${DOMAIN}"
fi
CURRENT_IPV6=$(dig AAAA ${SUBDOMAIN} +short)
if [ "${CURRENT_IPV6}" = "${WAN_IPV6}" ] ; then
echo "$(date "+[%Y-%m-%d %H:%M:%S]") [INFO] Current DNS AAAA record for ${RECORD} matches WAN IP (${CURRENT_IPV6}). Nothing to do."
continue
fi
DATA='{"rrset_ttl": '${TTL}', "rrset_values": ["'${WAN_IPV6}'"]}'
status=$(curl -s -w %{http_code} -o /dev/null -XPUT -d "${DATA}" \
-H"Authorization: Bearer ${GANDI_PAT}" \
-H"Content-Type: application/json" \
"${API}/domains/${DOMAIN}/records/${RECORD}/AAAA")
if [ "${status}" = '201' ] ; then
echo "$(date "+[%Y-%m-%d %H:%M:%S]") [OK] Updated ${RECORD} to ${WAN_IPV6}"
else
echo "$(date "+[%Y-%m-%d %H:%M:%S]") [ERROR] API POST returned status ${status}"
fi
done