-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckMyExternalDNS.sh
executable file
·88 lines (79 loc) · 3.58 KB
/
checkMyExternalDNS.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
#!/bin/bash
#######################################################################################################################
#
# Script to check whether current external IP address matches the IP address registered in a dyndns service
# and update the dyndns IP address if there is a mismatch
# Useful to start per cron early in the morning when the daily IP renewal happend in Germany
#
#######################################################################################################################
#
# Copyright (c) 2023 framp at linux-tips-and-tricks dot de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#######################################################################################################################
MYNAME=$(basename $0)
MYNAME=${MYNAME%.sh}
MYCONFIG="/usr/local/etc/$MYNAME.conf"
LOG="/var/log/${MYNAME}.log"
if [[ ! -f $MYCONFIG ]]; then
echo "Config file $MYCONFIG not found. Using hard coded values"
else
EXTERNAL_NAME="myName.ddns.org" # external DYNDNS name
USERNAME="[email protected]" # DYNDNS username
PWD="myVerySecurePassword" # DYNDNS password
DDNS_URL="ddns.org/update" # DNYDNS URL to update the registered IP
fi
source $MYCONFIG
STATE_NOW="$(date +%Y%m%d)"
STATE_FILE="/tmp/${MYNAME}.sts"
NOW=$(date)
UPDATE_RETRY=3
UPDATE_INTERVAL=60 # seconds
touch $STATE_FILE
lastRun=$(< $STATE_FILE)
if [[ -z $lastRun ]] || (( "$lastRun" < "$STATE_NOW" )); then
myExternalIP="$(dig +short myip.opendns.com @resolver1.opendns.com)"
externalNameIP="$(getent hosts $EXTERNAL_NAME | cut -f 1 -d " ")"
success=0
if [[ "$myExternalIP" != "$externalNameIP" ]]; then
for (( i=0; i<=$UPDATE_RETRY; i++)); do
echo "$NOW: Update request $i from $externalNameIP to $myExternalIP" >> $LOG
curl -s "$DDNS_URL?hostname=$EXTERNAL_NAME&myip=$myExternalIP&user=$USERNAME&pass=$PWD"
if (( $? )); then
echo "$NOW: Update request $i failed" >> $LOG
if (( $i != $UPDATE_RETRY )); then
sleep ${UPDATE_INTERVAL}s
NOW=$(date)
else
break
fi
else
success=1
break
fi
done
if (( $success )); then
echo "$NOW: IP OK $externalNameIP - $myExternalIP" >> $LOG
echo "$STATE_NOW" > $STATE_FILE
else
echo "$NOW: IP update failed from $externalNameIP to $myExternalIP" >> $LOG
fi
else
echo "$NOW: IP OK $externalNameIP - $myExternalIP" >> $LOG
echo "$STATE_NOW" > $STATE_FILE
fi
else
: echo "$NOW: Skipped" >> $LOG
fi