forked from filiparag/hetzner_ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hetzner_ddns.sh
executable file
·221 lines (204 loc) · 6.33 KB
/
hetzner_ddns.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
#!/bin/sh
self='hetzner_ddns'
daemon=0
for arg in $(seq "$#"); do
param="$(eval "echo \$$arg")"
case "$param" in
'--daemon'|'-d')
daemon=1;;
'--help'|'-h')
man hetzner_ddns;
exit 0;;
*)
self="${self}.$param";;
esac
done
# Read variabels from configuration file
if test -G "/usr/local/etc/$self.conf"; then
. "/usr/local/etc/$self.conf"
records_escaped="$(echo "$records" | sed 's:\*:\\\*:g')"
else
>&2 echo "unable to read configuration file /usr/local/etc/$self.conf"
exit 78
fi
# Check dependencies
if ! command -v curl > /dev/null || \
! command -v awk > /dev/null || \
! command -v jq > /dev/null
then
>&2 echo 'missing dependency'
exit 1
fi
# Check logging support
if ! touch "/var/log/$self.log";
then
>&2 echo 'unable to open logfile'
exit 2
fi
get_zone() {
# Get zone ID
zone="$(
curl "https://dns.hetzner.com/api/v1/zones" \
-H "Auth-API-Token: $key" 2>/dev/null | \
jq -r '.zones[] | .name + " " + .id' | \
awk "\$1==\"$domain\" {print \$2}"
)"
if [ -z "$zone" ]; then
return 1
else
printf '[%s] Zone for %s: %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" \
"$domain" "$zone" | tee -a "/var/log/$self.log"
fi
}
get_record() {
# Get record IDs
if [ -n "$zone" ]; then
record_ipv4="$(
curl "https://dns.hetzner.com/api/v1/records?zone_id=$zone" \
-H "Auth-API-Token: $key" 2>/dev/null | \
jq -r '.records[] | .name + " " + .type + " " + .id' | \
awk "\$1==\"$1\" && \$2==\"A\" {print \$3}"
)"
record_ipv6="$(
curl "https://dns.hetzner.com/api/v1/records?zone_id=$zone" \
-H "Auth-API-Token: $key" 2>/dev/null | \
jq -r '.records[] | .name + " " + .type + " " + .id' | \
awk "\$1==\"$1\" && \$2==\"AAAA\" {print \$3}"
)"
fi
if [ -z "$record_ipv4" ] && [ -z "$record_ipv6" ]; then
return 1
else
printf '[%s] IPv4 record for %s: %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1.$domain" \
"${record_ipv4:-(missing)}" | tee -a "/var/log/$self.log"
printf '[%s] IPv6 record for %s: %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1.$domain" \
"${record_ipv6:-(missing)}" | tee -a "/var/log/$self.log"
fi
}
get_records() {
# Get all record IDs
for n in $records_escaped; do
n="$(echo "$n" | sed 's:\\::')"
if get_record "$n"; then
records_ipv4="$records_ipv4$n=$record_ipv4 "
records_ipv6="$records_ipv6$n=$record_ipv6 "
else
printf '[%s] Missing both records for %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" \
"$n.$domain" | tee -a "/var/log/$self.log"
fi
done
}
get_record_ip_addr() {
# Get record's IP address
if [ -n "$record_ipv4" ]; then
ipv4_rec="$(
curl "https://dns.hetzner.com/api/v1/records/$record_ipv4" \
-H "Auth-API-Token: $key" 2>/dev/null | \
jq -r '.record.value'
)"
fi
if [ -n "$record_ipv6" ]; then
ipv6_rec="$(
curl "https://dns.hetzner.com/api/v1/records/$record_ipv6" \
-H "Auth-API-Token: $key" 2>/dev/null | \
jq -r '.record.value'
)"
fi
if [ -z "$ipv4_rec" ] && [ -z "$ipv6_rec" ]; then
return 1
fi
}
get_my_ip_addr() {
# Get current public IP address
ipv4_cur="$(
curl -4 'https://ip.hetzner.com/' 2>/dev/null
)"
ipv6_cur="$(
curl -6 'https://ip.hetzner.com/' 2>/dev/null | sed 's/:$/:1/g'
)"
if [ -z "$ipv4_cur" ] && [ -z "$ipv6_cur" ]; then
return 1
fi
}
set_record() {
# Update record if IP address has changed
if [ -n "$record_ipv4" ] && [ -n "$ipv4_cur" ] && [ "$ipv4_cur" != "$ipv4_rec" ]; then
curl -X "PUT" "https://dns.hetzner.com/api/v1/records/$record_ipv4" \
-H 'Content-Type: application/json' \
-H "Auth-API-Token: $key" \
-d "{
\"value\": \"$ipv4_cur\",
\"ttl\": $interval,
\"type\": \"A\",
\"name\": \"$n\",
\"zone_id\": \"$zone\"
}" 1>/dev/null 2>/dev/null &&
printf "[%s] Update IPv4 for %s: %s => %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" \
"$n.$domain" "$ipv4_rec" "$ipv4_cur" | tee -a "/var/log/$self.log"
fi
if [ -n "$record_ipv6" ] && [ -n "$ipv6_cur" ] && [ "$ipv6_cur" != "$ipv6_rec" ]; then
curl -X "PUT" "https://dns.hetzner.com/api/v1/records/$record_ipv6" \
-H 'Content-Type: application/json' \
-H "Auth-API-Token: $key" \
-d "{
\"value\": \"$ipv6_cur\",
\"ttl\": $interval,
\"type\": \"AAAA\",
\"name\": \"$n\",
\"zone_id\": \"$zone\"
}" 1>/dev/null 2>/dev/null &&
printf "[%s] Update IPv6 for %s: %s => %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" \
"$n.$domain" "$ipv6_rec" "$ipv6_cur" | tee -a "/var/log/$self.log"
fi
}
pick_record() {
# Get record ID from array
echo "$2" | \
awk "{
for(i=1;i<=NF;i++){
n=\$i;gsub(/=.*/,\"\",n);
r=\$i;gsub(/.*=/,\"\",r);
if(n==\"$1\"){
print r;break
}
}}"
}
set_records() {
# Get my public IP address
if get_my_ip_addr; then
# Update all records if possible
for n in $records_escaped; do
n="$(echo "$n" | sed 's:\\::')"
record_ipv4="$(pick_record "$n" "$records_ipv4")"
record_ipv6="$(pick_record "$n" "$records_ipv6")"
if [ -n "$record_ipv4" ] || [ -n "$record_ipv6" ]; then
get_record_ip_addr && set_record
fi
done
fi
}
run_ddns() {
printf '[%s] Started Hetzner DDNS daemon\n' "$(date '+%Y-%m-%d %H:%M:%S')" \
| tee -a "/var/log/$self.log"
while ! get_zone || ! get_records; do
sleep $((interval/2+1))
done
while true; do
set_records
sleep "$interval"
done
}
if [ "$daemon" = '1' ]; then
# Deamonize and write PID to file
if touch "/var/run/$self.pid";
then
run_ddns &
echo $! > "/var/run/$self.pid"
else
>&2 echo 'unable to daemonize'
exit 2
fi
else
# Run in foreground
run_ddns
fi