-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcp_dyndns.sh
executable file
·234 lines (206 loc) · 5.55 KB
/
gcp_dyndns.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env bash
#===============================================================================
#
# FILE: gcp_dyndns.sh
# USAGE: ./gcp_dyndns.sh [options]
# DESCRIPTION: A script to enable automated updates to a record in
# Google Cloud DNS for usage similar to DYNDNS
# OPTIONS: -d [FQDN]
# -q
# -t [TTL]
# -r [RECORD TYPE]
# -z [ZONE NAME]
# REQUIREMENTS: dig, and google cloud sdk fully authenticated
# BUGS: they will be discovered at random times
# NOTES:
# AUTHOR: Cesar B. (), cesar@poa.nyc
# ORGANIZATION: poa.nyc
# CREATED: 2022-11-20
# REVISION: 6
# LICENSE: Copyright (c) 2022, cesar@poa.nyc
# All rights reserved.
#
# This source code is licensed under the BSD-style license
# found in the LICENSE file in the root directory of this
# source tree.
#
#===============================================================================
LC_ALL=C
LANG=C
set -e
set -o nounset
set -o pipefail
set -u
readonly PROGNAME=$(basename $0)
readonly PROGIDR=$(readlink -m $(dirname $0))
trap 'echo "${NAME}: Ouch! Quitting." 1>&2 ; exit 1' 1 2 3 9 15
function main()
{
SILENT="0"
readonly RED=$(tput setaf 1)
readonly BLU=$(tput setaf 4)
readonly GRN=$(tput setaf 40)
readonly CLR=$(tput sgr0)
local _DEPS="dig"
for ITER in ${_DEPS}
do
if [ -z "$(which ${ITER} 2>/dev/null)" ]
then
printf "%s\n" \
"${RED}[X] ${ITER} not found.${CLR}"
exit 1
else
readonly ${ITER^^}="$(which ${ITER})"
fi
done
}
function _NEW_ADDR()
{
readonly NEW_ADDR=$(\
${DIG} \
-4 \
TXT \
+short \
o-o.myaddr.l.google.com \
@ns1.google.com \
| tr -d "\"")
if [[ -z "${NEW_ADDR}" ]]
then
printf "%s\n" \
"${RED}[X] Can not get your ISP provided IP address.${CLR}"
exit 1
fi
}
function _CUR_ADDR()
{
readonly CUR_ADDR=$(\
${DIG} \
-4 \
+short \
${DOMAIN} \
| tr -d "\"")
if [[ -z "${CUR_ADDR}" ]]
then
printf "%s\n" \
"${RED}[X] Existing address is missing. Check your FQDN.${CLR}"
exit 1
fi
}
function _CHANGE_IP()
{
if [[ $(echo ${NEW_ADDR}) != $(echo ${CUR_ADDR}) ]]
then
if [[ ${SILENT} -eq 0 ]]
then
printf "%s\n" \
"${GRN}[X] Updating A record for ${DOMAIN} with ${NEW_ADDR}${CLR}"
fi
${GCP_PATH:-$(which gcloud)} \
dns \
record-sets \
update \
${DOMAIN} \
--rrdatas=${NEW_ADDR} \
--ttl=${TTL:-300} \
--type=${RECORD:-A} \
--zone=${ZONE} \
2> >(sed $'s,.*,\e[31m&\e[m,'>&2) 1> /dev/null
if [[ $? -eq 1 ]]
then
exit 1
fi
else
if [[ ${SILENT} -eq 0 ]]
then
printf "%s\n" \
"${GRN}[X] Adresses are the same..${CLR}"
fi
fi
}
function _USAGE()
{
clear
echo -e "
NAME
${PROGNAME}
SYNOPSIS
${PROGNAME} [OPTION]...
DESCRIPTION
A script that reads your current IP address as assigned by your isp to
enable automated updates to the A record in Google Cloud DNS for usage
similar to DYNDNS
OPTIONS
-d [FQDN]
The DNS name that matches the incoming queries with this zone's
DNS name as its suffix.
This can be found in the second column by running :
gcloud dns managed-zones list
-q
Do not output messages. Set this flag if you want to run this
script in cron. This will not stop error messages. By default
this is not enabled.
-t [TTL]
The TTL in seconds that the resolver caches this resource
record set.
This defaults to 300 (5 minutes).
-r [resource type]
The resource record type of this resource record set.
This defaults to A.
-z [zone name]
The managed zone that this resource record set is affiliated with.
For example, my-zone-name; the name of this resource record set
must have the DNS name of the managed zone as its suffix.
This can be found in the first column by running :
gcloud dns managed-zones list
Examples
Update the A record for FQDN FOOBAR.BAZ with zone name EX-SET :
${PROGNAME} -d foobar.baz -z EX-SET
Requirement
This script requires that the Google Cloud SDK tools are installed and
configured in your PATH and that the command gcloud is functioning
against your Google Cloud account with access to Google Coud DNS.
"
}
main
## option selection
while getopts "d:qt:r:z:" OPT
do
case "${OPT}" in
'd')
## FQDN
DOMAIN=${OPTARG}
;;
'q')
## This sets quiet mode
SILENT="1"
;;
't')
## TTL time. This defaults to 300.
TTL=${OPTARG}
;;
'r')
## Record type. This defaults to A type.
RECORD=${OPTARG}
;;
'z')
## Google cloud dns zone name.
ZONE=${OPTARG}
;;
esac
done
if [[ ${OPTIND} -eq 1 ]]
then
_USAGE \
| less
exit 0
fi
shift $((OPTIND-1))
if [[ -z "${DOMAIN+x}" || -z "${ZONE+x}" ]]
then
_USAGE \
| less
exit 1
fi
_NEW_ADDR
_CUR_ADDR
_CHANGE_IP