-
Notifications
You must be signed in to change notification settings - Fork 2
/
no-ip-updater.tmp
177 lines (153 loc) · 5.28 KB
/
no-ip-updater.tmp
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
#!/bin/bash
# Module Header
MODULENAME=""
MODULEVERS=""
MODULECRED=""
modcreds;
# End of Module Header
#==========================================
# This WAS NOT CODED by Private-Locker or REDD
# This is for demostration purposes.
#==========================================
# Copyright (C) 2016 Matthew D. Mower
#
# 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.
# Defines
CONFIGFILE="$( cd "$( dirname "$0" )" && pwd )noipconfig/config"
if [ -e $CONFIGFILE ]; then
source $CONFIGFILE
else
echo "Config file not found."
exit 1
fi
if [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
echo "USERNAME or PASSWORD has not been set in the config file."
exit 1
fi
USERAGENT="Bash No-IP Updater/0.9 "$USERNAME
USERNAME=$(echo -ne $USERNAME | od -A n -t x1 | tr -d '\n' | sed 's/ /%/g')
PASSWORD=$(echo -ne $PASSWORD | od -A n -t x1 | tr -d '\n' | sed 's/ /%/g')
if [ ! -d $LOGDIR ]; then
mkdir -p $LOGDIR
if [ $? -ne 0 ]; then
echo "Log directory could not be created or accessed."
exit 1
fi
fi
LOGFILE=${LOGDIR%/}/noip.log
IPFILE=${LOGDIR%/}/last_ip
if [ ! -e $LOGFILE ] || [ ! -e $IPFILE ]; then
touch $LOGFILE $IPFILE
if [ $? -ne 0 ]; then
echo "Log files could not be created. Is the log directory writable?"
exit 1
fi
elif [ ! -w $LOGFILE ] || [ ! -w $IPFILE ]; then
echo "Log files not writable."
exit 1
fi
# Functions
# 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
}
# Program
NOW=$(date '+%s')
if [ -e $LOGFILE ] && tail -n1 $LOGFILE | grep -q -m1 '(abuse)'; then
echo "This account has been flagged for abuse. You need to contact noip.com to resolve"
echo "the issue. Once you have confirmed your account is in good standing, remove the"
echo "log line containing (abuse) from:"
echo " $LOGFILE"
echo "Then, re-run this script."
exit 1
fi
if [ -e $LOGFILE ] && tac $LOGFILE | grep -q -m1 '(911)'; then
NINELINE=$(tac $LOGFILE | grep -m1 '(911)')
LASTNL=$([[ $NINELINE =~ \[(.*?)\] ]] && echo "${BASH_REMATCH[1]}")
LASTCONTACT=$(date -d "$LASTNL" '+%s')
if [ `expr $NOW - $LASTCONTACT` -lt 1800 ]; then
LOGDATE="[$(date +'%Y-%m-%d %H:%M:%S')]"
LOGLINE="Response code 911 received less than 30 minutes ago; canceling request."
echo $LOGLINE
echo "$LOGDATE $LOGLINE" >> $LOGFILE
exit 1
fi
fi
GET_IP_URLS[0]="https://api.ipify.org"
GET_IP_URLS[1]="http://icanhazip.com"
GET_IP_URLS[2]="http://wtfismyip.com/text"
GET_IP_URLS[3]="http://nst.sourceforge.net/nst/tools/ip.php"
GIP_INDEX=0
while [ -n "${GET_IP_URLS[$GIP_INDEX]}" ] && ! valid_ip $NEWIP; do
NEWIP=$(curl -s ${GET_IP_URLS[$GIP_INDEX]} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
let GIP_INDEX++
done
if ! valid_ip $NEWIP; then
LOGDATE="[$(date +'%Y-%m-%d %H:%M:%S')]"
LOGLINE="Could not find current IP"
echo $LOGLINE
echo "$LOGDATE $LOGLINE" >> $LOGFILE
exit 1
fi
RESPONSE=$(curl -s -k --user-agent "$USERAGENT" "https://$USERNAME:$PASSWORD@dynupdate.no-ip.com/nic/update?hostname=$HOST&myip=$NEWIP")
RESPONSE=$(echo $RESPONSE | tr -cd "[:print:]")
RESPONSE_A=$(echo $RESPONSE | awk '{ print $1 }')
case $RESPONSE_A in
"good")
RESPONSE_B=$(echo $RESPONSE | awk '{ print $2 }')
LOGLINE="(good) DNS hostname(s) successfully updated to $RESPONSE_B."
;;
"nochg")
RESPONSE_B=$(echo $RESPONSE | awk '{ print $2 }')
LOGLINE="(nochg) IP address is current: $RESPONSE_B; no update performed."
;;
"nohost")
LOGLINE="(nohost) Hostname supplied does not exist under specified account. Revise config file."
;;
"badauth")
LOGLINE="(badauth) Invalid username password combination."
;;
"badagent")
LOGLINE="(badagent) Client disabled - No-IP is no longer allowing requests from this update script."
;;
"!donator")
LOGLINE="(!donator) An update request was sent including a feature that is not available."
;;
"abuse")
LOGLINE="(abuse) Username is blocked due to abuse."
;;
"911")
LOGLINE="(911) A fatal error on our side such as a database outage. Retry the update in no sooner than 30 minutes."
;;
*)
LOGLINE="(error) Could not understand the response from No-IP. The DNS update server may be down."
;;
esac
LOGDATE="[$(date +'%Y-%m-%d %H:%M:%S')]"
echo "IP: $NEWIP"
echo $NEWIP > $IPFILE
echo $LOGLINE
echo "$LOGDATE $LOGLINE" >> $LOGFILE
exit 0