This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsupdate
executable file
·162 lines (143 loc) · 4.39 KB
/
dnsupdate
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
#!/bin/bash
# Configuration ################################################################
USERNAME="" # Your username at INWX
PASSWORD="" # Your password at INWX
V4_RECORD_ID="" # The ID of the A record
V6_RECORD_ID="" # The ID of the AAAA record
SILENT=false # Should the script write a logfile? (true | false)
################################################################################
API_ENDPOINT="https://api.domrobot.com/xmlrpc/"
V4_POOL=(
"https://ip4.nnev.de/"
"http://v4.ident.me/"
"https://ipv4.icanhazip.com"
"https://v4.ifconfig.co/"
"https://ipv4.wtfismyip.com/text"
)
V6_POOL=(
"https://ip6.nnev.de/"
"http://v6.ident.me/"
"https://ipv6.icanhazip.com"
"https://v6.ifconfig.co/"
"https://ipv6.wtfismyip.com/text"
)
UPDATE_RECORD=$(cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>nameserver.updateRecord</methodName>
<params>
<param>
<value>
<struct>
<member>
<name>user</name>
<value>
<string>%USER%</string>
</value>
</member>
<member>
<name>lang</name>
<value>
<string>en</string>
</value>
</member>
<member>
<name>pass</name>
<value>
<string>%PASSWD%</string>
</value>
</member>
<member>
<name>id</name>
<value>
<int>%DNSID%</int>
</value>
</member>
<member>
<name>content</name>
<value>
<string>%NEWIP%</string>
</value>
</member>
<member>
<name>ttl</name>
<value>
<int>360</int>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>
EOF
)
function v4() {
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
continue
done
return 1
}
function v6() {
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
continue
done
return 1
}
function log() {
$SILENT || echo "$(date --utc) | $1" | tee -a update.log
}
function update_ip() {
DATA=$(echo "$UPDATE_RECORD" | sed -e "s/%USER%/$1/g;s/%PASSWD%/$2/g;s/%DNSID%/$3/g;s/%NEWIP%/$4/g")
curl -s -X POST -d "$DATA" "$API_ENDPOINT" --header "Content-Type:text/xml"
}
touch old.ipv4 old.ipv6
OLD_V4=$(cat old.ipv4)
OLD_V6=$(cat old.ipv6)
# Write "(empty)" if the files are empty for nice output on first run.
if [ -z "$OLD_V4" ]; then OLD_V4="(empty)"; fi
if [ -z "$OLD_V6" ]; then OLD_V6="(empty)"; fi
NEW_V4=$(v4)
if [[ $? == 1 ]]; then
log "Could not get a valid IPv4 address from the pool. Is the connection up?"
exit 1
fi
NEW_V6=$(v6)
if [[ $? == 1 ]]; then
log "Could not get a valid IPv6 address from the pool. Is the connection up?"
exit 1
fi
# Update the A record
if [ ! "$OLD_V4" == "$NEW_V4" ]; then
UPDATE_IP_OUTPUT=$(update_ip "$USERNAME" "$PASSWORD" "$V4_RECORD_ID" "$NEW_V4")
if ! grep -q "Command completed successfully" <<< "$UPDATE_IP_OUTPUT"; 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 "$NEW_V4" > old.ipv4
log "Updated IPv4: $OLD_V4 --> $NEW_V4"
else
echo "IPv4: No changes"
fi
# Update the AAAA record
if [ ! "$OLD_V6" == "$NEW_V6" ]; then
UPDATE_IP_OUTPUT=$(update_ip "$USERNAME" "$PASSWORD" "$V6_RECORD_ID" "$NEW_V6")
if ! grep -q "Command completed successfully" <<< "$UPDATE_IP_OUTPUT"; 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 "$NEW_V6" > old.ipv6
log "Updated IPv6: $OLD_V6 --> $NEW_V6"
else
echo "IPv6: No changes"
fi