-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_ip_update.sh
executable file
·42 lines (39 loc) · 1.52 KB
/
dynamic_ip_update.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
#!/bin/bash
#
# Author: Nate Levesque <[email protected]>
# Language: Shell
# Filename: dynamic_ip_update.sh
#
# Description:
# Fetches a URL to update the IP record of the system remotely
# in order to keep the remote record of a dynamic Ip address up to date.
# Intended to be used as a cron or other schedule job, but can also
# be run manually.
#
URL==
logFile=$HOME/dyndns.log
Error(){
# Reflects an error retrieving the URL and logs it to the specified
# logFile with the date and time
echo "`date`: ERROR: Could not update dynamic IP address" >> $logFile
}
Success(){
# Reflects success retrieving the URL and logs it as well as the
# current IP address to the specified logFile with the date and time
echo "`date`: SUCCESS: Updated dynamic IP address to `curl icanhazip.com`" >> $logFile
}
CheckIP(){
# Figures out the previous IP address update by looking at the log file, and exits if it is
# the same as the current one, reflecting this in the log
lastIP=`awk '/SUCCESS/ { save=$NF }END{ print save }' $logFile 2>/dev/null`
currIP=`curl -s --connect-timeout 5 icanhazip.com`
# Checks if a logfile exists and if the last and current IP's are the same,
# and pulls the url if either happens to not be the case.
if [ ! "$lastIP" = "$currIP" ] || [ ! -e $logFile ]; then
curl -s --connect-timeout 5 $URL && Success || Error
else
echo "`date`: OK: IP address ($currIP), has not changed since last check." >> $logFile
fi
}
# Runs the whole thing
CheckIP