-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdnsupdate.sh
executable file
·174 lines (151 loc) · 5.03 KB
/
dnsupdate.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
#!/bin/bash
# curl -s = silent mode
# if [ -z "$OLDIPv4" ] = true if $OLDIPv4 (a string) is null -> https://stackoverflow.com/a/18096739/11458487
# Configuration ################################################################
USERNAME="" # Your username at INWX
PASSWORD="" # Your password at INWX
DNSIDsv4=("1" "2") # The ID of the A record
DNSIDsv6=() # The ID of the AAAA record
SILENT=false # Should the script write a logfile? (true | false)
UPDATEURLv4="" # Your prefered host to get the IPv4 from
UPDATEURLv6="" # Your prefered host to get the IPv6 from
################################################################################
DNSIDS=("${DNSIDsv4[@]}" "${DNSIDsv6[@]}") # Concat the two arrays
APIHOST="https://api.domrobot.com/xmlrpc/" # API URL from inwx.de
# Define functions #############################################################
function log() {
# Only log if $SILENT is false
$SILENT || echo "$(date) | $1" >> update.log
}
function get_v4_ip() {
if [ ! -z "$UPDATEURLv4" ]
then
log "Host defined, get IP from $UPDATEURLv4"
# get this from https://unix.stackexchange.com/a/20793
echo $(host $UPDATEURLv4 | awk '/has address/ { print $4 ; exit }')
return 0
fi
if [ ! -e v4.pool ]
then
log "No IPv4 pool (v4.pool file) found. Using https://ip4.ident.me/"
echo $(curl -s "https://v4.ident.me")
return 0
fi
V4_POOL=$(cat v4.pool)
for V4_API in $V4_POOL; do
MAYBE_V4_ADDR=$(curl -s "$V4_API")
if [[ $MAYBE_V4_ADDR =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "$MAYBE_V4_ADDR"
return 0
fi
done
return 1
}
function get_v6_ip() {
if [ ! -z "$UPDATEURLv6" ]
then
log "Host defined, get IP from $UPDATEURLv6"
# get this from https://unix.stackexchange.com/a/20793
echo $(host $UPDATEURLv6 | awk '/has address/ { print $4 ; exit }')
return 0
fi
if ! [ -e v6.pool ]
then
log "No IPv6 pool (v6.pool file) found. Using https://ip6.ident.me/"
echo $(curl -s "https://v6.ident.me/")
return 0
fi
V6_POOL=$(cat v6.pool)
for V6_API in $V6_POOL; do
MAYBE_V6_ADDR=$(curl -s "$V6_API")
if [[ $MAYBE_V6_ADDR == *":"* ]]; then
echo "$MAYBE_V6_ADDR"
return 0
fi
done
return 1
}
# check if the IPv4/6 array exists, then create old.ipv4/6 if not available
# finally get recent IPv4/IPv6
if [ ${#DNSIDsv4[@]} -ne 0 ]
then
if ! [ -e old.ipv4 ]
then
touch old.ipv4
fi
OLDIPv4=$(cat old.ipv4)
fi
if [ ${#DNSIDsv6[@]} -ne 0 ]
then
if ! [ -e old.ipv6 ]
then
touch old.ipv6
fi
OLDIPv6=$(cat old.ipv6)
fi
################################################################################
# Do the update if needed ######################################################
for DNSID in ${DNSIDS[@]}; do
# checking if the entry is v4 or not, see https://stackoverflow.com/a/15394738/11458487
if [[ " ${DNSIDsv4[@]} " =~ " ${DNSID} " ]]; then
ISIPv4=true
log "Entry $DNSID, v4 starts"
else
ISIPv4=false
log "Entry $DNSID, v6 starts"
fi
# Write "(empty)" if the files are empty for nice output on first run.
if [ -z "$OLDIPv4" ]; then OLDIPv4="(empty)"; fi
if [ -z "$OLDIPv6" ]; then OLDIPv6="(empty)"; fi
# get actual IPv4/IPv6
if [[ "$ISIPv4" = true ]]; then
NEWIPv4=$(get_v4_ip)
if [[ $? == 1 ]]; then
echo $NEWIPv4
log "Could not get a valid IPv4 address from the pool or URL. Is the connection up?"
exit 1
fi
# update the A-record
if [ ! "$OLDIPv4" == "$NEWIPv4" ]; then
log "Updating IPv4 to $NEWIPv4"
DATA=$(cat update.api | sed "s/%PASSWD%/$PASSWORD/g;s/%USER%/$USERNAME/g;s/%DNSID%/$DNSID/g;s/%NEWIP%/$NEWIPv4/g")
RET=$(curl -s -X POST -d "$DATA" "$APIHOST" --header "Content-Type:text/xml")
if ! grep -q "Command completed successfully" <<< "$RET"; then
log "Something went wrong updating the IPv4 address. Check the configuration and make sure you're not using Two-Factor-Authentication."
exit 1
fi
echo $NEWIPv4 > old.ipv4
log "Updated IPv4: $OLDIPv4 --> $NEWIPv4"
else
log "IPv4: No changes"
fi
else
log "Skipping IPv4: No DNS record ID set"
fi
if [[ "$ISIPv4" = false ]]; then
NEWIPv6=$(get_v6_ip)
if [[ $? == 1 ]]; then
log "Could not get a valid IPv6 address from the pool or URL. Is the connection up?"
exit 1
fi
# update the AAAA-record
if [ ! "$OLDIPv6" == "$NEWIPv6" ]; then
log "Updating IPv6 to $NEWIPv6"
DATA=$(cat update.api | sed "s/%PASSWD%/$PASSWORD/g;s/%USER%/$USERNAME/g;s/%DNSID%/$DNSID/g;s/%NEWIP%/$NEWIPv6/g")
RET=$(curl -s -X POST -d "$DATA" "$APIHOST" --header "Content-Type:text/xml")
if ! grep -q "Command completed successfully" <<< "$RET"; then
log "Something went wrong updating the IPv6 address. Check the configuration and make sure you're not using Two-Factor-Authentication."
exit 1
fi
echo $NEWIPv6 > old.ipv6
log "Updated IPv6: $OLDIPv6 --> $NEWIPv6"
else
log "IPv6: No changes"
fi
else
log "Skipping IPv6: No DNS record ID set"
fi
log "Entry $DNSID finished"
log "###################################################"
done
################################################################################