This repository has been archived by the owner on May 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip-updater
executable file
·240 lines (197 loc) · 5.23 KB
/
ip-updater
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
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# IP Change Detector and Notifier.
# https://github.com/ZacharyDuBois/IP-Updater
version="v0.2"
##
# Variables
##
# API Information
digitalOceanAPI=""
# DNS Info
domain=""
recordID1=""
recordName1=""
# The next two are optional
recordID2=""
recordName2=""
# Log Location (Must be absolute)
logLocation=""
# If you don't want to use Pushover, leave pushoverAppKey and/or pushoverUserKey set to "".
pushoverAppKey=""
pushoverUserKey=""
pushoverTitle="IP Updater"
pushoverURL=""
pushoverURLTitle=""
# Other variables that should not need to be changed have been moved below Startup Checks.
##
# Startup Checks
##
# Check if root
if [[ "$(id -u)" == "0" ]]
then
echo "Do not run this script as root."
exit 1
fi
# Check for curl
which curl > /dev/null
checkForCurl=$?
if [[ "$checkForCurl" != "0" ]]
then
echo "You need curl to run this script."
exit 1
fi
# Check for variables set correctly
if [[ "$digitalOceanAPI" == "" ]] && [[ "$domain" == "" ]] && [[ "$recordID1" == "" ]] && [[ "$recordName1" == "" ]] && [[ "$logLocation" == "" ]]
then
echo "You have not set all the required variables in the variables block. Please edit them with the correct details."
exit 1
fi
# Check if locations and files exist.
if ! [[ -r "$logLocation" ]]
then
echo "The log location does not exist. Attempting to create one."
echo "YYYY-MM-DD HH:MM:SS 127.0.0.1 X X X" > $logLocation
logCreateStatus=$?
if [[ "$logCreateStatus" != "0" ]]
then
echo "Failed to make the log file. Exitting."
exit 1
fi
fi
# Other (You should not need to modify these)
ip="$(curl -s http://icanhazip.com/)"
lastLogLine="$(tail -n 1 $logLocation)"
lastIP="$(echo $lastLogLine | awk -F' ' '{ print $3 }')"
time="$(date '+%F %T')"
##
# Start
##
pushoverSend() {
message=$1
if [[ "$pushoverAppKey" != "" ]] && [[ "$pushoverUserKey" != "" ]]
then
curl -s --form-string "token=$pushoverAppKey" --form-string "user=$pushoverUserKey" --form-string "url=$pushoverURL" --form-string "url_title=$pushoverURLTitle" --form-string "priority=0" --form-string "title=$pushoverTitle" --form-string "message=$message" https://api.pushover.net/1/messages.json > /dev/null
pushoverStatus=$?
if [[ "$pushoverStatus" == "0" ]]
then
echo "Pushover message sent successfully: $message"
else
echo "Pushover failed to send message: $message"
fi
else
echo "No Pushover key set to send message: $message"
fi
}
dnsUpdate() {
updatedIP=$1
forceUpdate=$2
echo "Updating DNS1 with $updatedIP."
curl -s -X PUT "https://api.digitalocean.com/v2/domains/$domain/records/$recordID1" -d'{"name":"'$recordName1'", "data":"'$ip'"}' -H "Authorization: Bearer $digitalOceanAPI" -H "Content-Type: application/json" > /dev/null
dns1=$?
if [[ "$recordID2" != "" ]] && [[ "$recordName2" != "" ]] || [[ "$forceUpdate" == "Y" ]]
then
echo "Updating DNS2 with $updatedIP."
curl -s -X PUT "https://api.digitalocean.com/v2/domains/$domain/records/$recordID1" -d'{"name":"'$recordName1'", "data":"'$ip'"}' -H "Authorization: Bearer $digitalOceanAPI" -H "Content-Type: application/json" > /dev/null
dns2=$?
else
dns2=""
fi
}
logMSG="$time $ip"
logInformation() {
statusCode=$1
if [[ "$statusCode" == "" ]]
then
logStatus="C"
elif [[ "$statusCode" == "0" ]]
then
logStatus="+"
else
logStatus="X"
fi
logMSG="$logMSG $logStatus"
}
##
# Retry if no IP
##
retryCount=0
if [[ "$ip" == "" ]]
then
while [[ "$ip" == "" ]] && [[ "$retryCount" < 3 ]]
do
if [[ "$retryCount" == 0 ]]
then
echo "IP is unavailable. Retrying once every 10 seconds for 30 seconds for IP."
else
retryCount=$((retryCount+1))
echo -n "X"
ip="$(curl -s http://icanhazip.com/)"
fi
sleep 10
done
if [[ "$retryCount" != 0 ]]
then
echo
fi
##
# Check for IP Changes
##
if [[ "$ip" == "" ]]
then
##
# No IP/Internet
##
ip="UNAVAILABLE"
echo "IP is currently unavailable."
else
##
# If IP Changed
##
echo "The IP has changed from $lastIP to $ip. Running updates."
# Notify via Pushover
pushoverSend "Your IP has changed! Your new IP is $ip (Changed from $lastIP)"
# Update the DNS
dnsUpdate "$ip"
fi
##
# Fail Check
##
lastStatus=()
lastStatusInt=4
actionList=('pushoverSend "Your IP has changed Your new IP is $ip (Changed from $lastIP)"', 'dnsUpdate "$ip"', 'dnsUpdate "$ip" "Y"')
actionListInt=0
echo "Checking for any failures from last run."
while [[ "$(echo $lastLogLine | awk -F' ' '{ print $"$lastStatusInt" }' )" != "" ]]
do
lastStatus+= "$(echo $lastLogLine | awk -F' ' '{ print $"$lastStatusInt" }' )"
lastStatusInt=$(($lastStatusInt+1))
done
for lastStat in "${lastStatus[@]}"
do
if [[ "$lastStat" == "X" ]]
then
echo "Failure found. Attempting correction."
eval "${actionList["$actionListInt"]}"
fi
actionListInt=$(($actionListInt+1))
done
##
# Write Log
##
logInformation "$pushoverStatus"
logInformation "$dns1"
logInformation "$dns2"
echo "Writing log file."
echo "Adding \"$logMSG\" to log."
echo "$logMSG" >> $logLocation
##
# Keep Log Small
##
if [[ "$(wc -l < $logLocation)" > 5000 ]]
then
echo "Log file is larger than 5000 lines. Removing oldest line."
sed -i '1d' $logLocation
fi
echo "Done."
exit 0