forked from AntonioCS/no-ip.com-bash-updater
-
Notifications
You must be signed in to change notification settings - Fork 29
/
noipupdater.sh
executable file
·292 lines (253 loc) · 8.04 KB
/
noipupdater.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
# Copyright (C) 2013 Matthew D. Mower
# Copyright (C) 2012 AntonioCS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -eo pipefail
# Functions
function usage() {
console_error "$0 [-c /path/to/config] [-i 123.123.123.123]"
exit 1
}
function cmd_exists() {
command -v "$1" > /dev/null 2>&1
}
# Adapted from one of the more readable answers by Gilles at
# https://unix.stackexchange.com/questions/60653/urlencode-function/60698#60698
function urlencode() {
local string="$1"
while [ -n "$string" ]; do
local tail="${string#?}"
local head="${string%"$tail"}"
case "$head" in
[-._~0-9A-Za-z])
printf "%c" "$head";;
*)
printf "%%%02x" "'$head"
esac
string="$tail"
done
}
function http_get() {
if cmd_exists curl; then
curl -s --user-agent "$USERAGENT" "$1"
elif cmd_exists wget; then
wget -q -O - --user-agent="$USERAGENT" "$1"
else
console_error "No http tool found. Install curl or wget."
exit 1
fi
}
function parse_date() {
local string="$1"
PARSED_DATE=$(date -d "$string" +'%s' 2>/dev/null || true)
if [ -z "$PARSED_DATE" ] && cmd_exists gdate; then
PARSED_DATE=$(gdate -d "$string" +'%s' 2>/dev/null || true)
fi
if [ -z "$PARSED_DATE" ]; then
PARSED_DATE=$(date -jf "%Y-%m-%d %H:%M:%S" "$string" +'%s' 2>/dev/null || true)
fi
if [ -z "$PARSED_DATE" ]; then
console_error "Could not parse date."
exit 1
fi
return 0
}
# IP Validator
# http://www.linuxjournal.com/content/validating-ip-address-bash-script
function valid_ip() {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
function get_logline() {
local host
local response
local response_a
local response_b
host="$1"
response=$(echo "$2" | tr -cd "[:print:]")
response_a=$(echo "$response" | awk '{ print $1 }')
case $response_a in
"good")
response_b=$(echo "$response" | awk '{ print $2 }')
LOGLINE="(good) [$host] DNS hostname successfully updated to $response_b."
;;
"nochg")
response_b=$(echo "$response" | awk '{ print $2 }')
LOGLINE="(nochg) [$host] IP address is current: $response_b; no update performed."
;;
"nohost")
LOGLINE="(nohost) [$host] Hostname supplied does not exist under specified account. Revise config file."
;;
"badauth")
LOGLINE="(badauth) [$host] Invalid username password combination."
;;
"badagent")
LOGLINE="(badagent) [$host] Client disabled - No-IP is no longer allowing requests from this update script."
;;
'!donator')
LOGLINE='(!donator)'" [$host] An update request was sent including a feature that is not available."
;;
"abuse")
LOGLINE="(abuse) [$host] Username is blocked due to abuse."
;;
"911")
LOGLINE="(911) [$host] A fatal error on our side such as a database outage. Retry the update in no sooner than 30 minutes."
;;
"")
LOGLINE="(empty) [$host] No response received from No-IP. This may be due to rate limiting or a server-side problem."
;;
*)
LOGLINE="(error) [$host] Could not understand the response from No-IP. The DNS update server may be down."
;;
esac
return 0
}
function console_info() {
local msg="$1"
local lvl="$CONSOLE_OUTPUT_LEVEL"
if [ -z "$lvl" ] || (( lvl < 3 )); then
echo "$msg"
fi
}
function console_error() {
local msg="$1"
local lvl="$CONSOLE_OUTPUT_LEVEL"
if [ -z "$lvl" ] || (( lvl < 5 )); then
echo "$msg" >&2
fi
}
# Defines
CONFIGFILE=""
NEWIP=""
while getopts 'c:i:' flag; do
case "${flag}" in
c) CONFIGFILE="${OPTARG}" ;;
i) NEWIP="${OPTARG}" ;;
*) usage ;;
esac
done
if [ -z "$CONFIGFILE" ]; then
CONFIGFILE="$( cd "$( dirname "$0" )" && pwd )/config"
fi
if [ -e "$CONFIGFILE" ]; then
source "$CONFIGFILE"
else
console_error "Config file not found."
exit 1
fi
if ! (( CONSOLE_OUTPUT_LEVEL >= 0 && CONSOLE_OUTPUT_LEVEL <= 6 )); then
CONSOLE_OUTPUT_LEVEL=0
fi
if [ -n "$NEWIP" ] && ! valid_ip "$NEWIP"; then
console_error "Invalid IP address specified."
exit 1
fi
if [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
console_error "USERNAME or PASSWORD has not been set in the config file."
exit 1
fi
USERAGENT="Bash No-IP Updater/1.3 $USERNAME"
if [ ! -d "$LOGDIR" ]; then
if ! mkdir -p "$LOGDIR"; then
console_error "Log directory could not be created or accessed."
exit 1
fi
fi
LOGFILE=${LOGDIR%/}/noip.log
if [ ! -e "$LOGFILE" ]; then
if ! touch "$LOGFILE"; then
console_error "Log files could not be created. Is the log directory writable?"
exit 1
fi
fi
if [ ! -w "$LOGFILE" ]; then
console_error "Log file not writable."
exit 1
fi
NOW=$(date +'%s')
LOGDATE="[$(date +'%Y-%m-%d %H:%M:%S')]"
# Program
if [ -e "$LOGFILE" ] && tail -n1 "$LOGFILE" | grep -q -m1 '(abuse)'; then
# set -e aborts after this read command unless `|| true` is appended
read -r -d '' CONSOLE_MSG << EOL || true
This account has been flagged for abuse. You need to contact noip.com to resolve
the issue. Once you have confirmed your account is in good standing, remove the
log line containing (abuse) from:
$LOGFILE
Then, re-run this script.
EOL
console_error "$CONSOLE_MSG"
exit 1
fi
if [ -e "$LOGFILE" ]; then
if NINELINE=$(grep '(911)' "$LOGFILE" | tail -n 1); then
LASTNL=$([[ "$NINELINE" =~ \[([^\]]+)\] ]] && echo "${BASH_REMATCH[1]}")
parse_date "$LASTNL"
if [ $((NOW - PARSED_DATE)) -lt 1800 ]; then
LOGLINE="Response code 911 received less than 30 minutes ago; canceling request."
console_error "$LOGLINE"
echo "$LOGDATE $LOGLINE" >> "$LOGFILE"
exit 1
fi
fi
fi
if [ "$ROTATE_LOGS" = true ]; then
LOGLENGTH=$(wc -l "$LOGFILE" | awk '{ print $1 }')
if [ $((LOGLENGTH)) -ge 10010 ]; then
BACKUPDATE=$(date +'%Y-%m-%d_%H%M%S')
BACKUPFILE="$LOGFILE-$BACKUPDATE.log"
if touch "$BACKUPFILE"; then
head -10000 "$LOGFILE" > "$BACKUPFILE"
if cmd_exists gzip; then
gzip "$BACKUPFILE"
fi
LASTLINES=$(tail -n +10001 "$LOGFILE")
echo "$LASTLINES" > "$LOGFILE"
console_info "Log file rotated"
else
console_error "Log file could not be rotated"
fi
fi
fi
USERNAME=$(urlencode "$USERNAME")
PASSWORD=$(urlencode "$PASSWORD")
ENCODED_HOST=$(urlencode "$HOST")
REQUEST_URL="https://$USERNAME:[email protected]/nic/update?hostname=$ENCODED_HOST"
if [ -n "$NEWIP" ]; then
NEWIP=$(urlencode "$NEWIP")
REQUEST_URL="$REQUEST_URL&myip=$NEWIP"
fi
RESPONSE=$(http_get "$REQUEST_URL")
OIFS=$IFS
IFS=$'\n'
SPLIT_RESPONSE=( $(echo "$RESPONSE" | grep -o '[0-9a-z!]\+\( [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\?') )
IFS=','
SPLIT_HOST=( $HOST )
IFS=$OIFS
for index in "${!SPLIT_HOST[@]}"; do
get_logline "${SPLIT_HOST[index]}" "${SPLIT_RESPONSE[index]}"
console_info "$LOGLINE"
echo "$LOGDATE $LOGLINE" >> "$LOGFILE"
done
exit 0