-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddns-tracker.sh
executable file
·138 lines (118 loc) · 3.12 KB
/
ddns-tracker.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
#!/bin/bash
#
# ddns-tracker.sh
# Intended to track DDNS address changes over time for a list of
# hosts and only recording changes from the last address.
# Updates:
# - added -q, quiet option. By default will now echo address changes to
# stdout
# Required utilities: cut, date, dig, grep, ping, tail, touch
# VARIABLES
site_list="ddns-tracker.conf"
output_file="ddns-tracker.csv"
#ping_host="google.com"
ping_cmd="ping -W 10 -t 10 -c 2"
quiet_updates=0
#
# FUNCTIONS
#
last_address() {
local site=$1
local last_addr
last_addr=$(grep $site $output_file | tail -n 1 | cut -d "," -f 2 )
if [ -z "$last_addr" ]; then
last_addr="missing"
fi
echo "$last_addr"
}
do_sites() {
local last_addr
while read site; do
current_addr=$(dig +short -t A $site)
if [ -z "$current_addr" ]; then
current_addr="lookup error"
fi
last_addr=$(last_address $site)
if [[ "$last_addr" == "missing" ]] || [[ "$current_addr" != "$last_addr" ]]; then
echo "$site,$current_addr,$(date)" >> $output_file
if [ "$quiet_updates" -ne 1 ]; then
echo -e "Address change for $site:\n Old:\t$last_addr\n New:\t$current_addr"
fi
fi
done < $site_list
}
usage() {
cat <<-EOF
Options:
-f [ sitefile ] list of sites to check [default=$site_list]
-o [ outfile ] output filename [default=$output_file]
-p [ ping_host ] ping host for connectivity test
-q quiet output, default reports changes to stdout
-h help
EOF
}
#
# MAIN
#
if [ "$#" -ne 0 ]; then
while getopts "f:o:p:qh" opt; do
case $opt in
"f")
site_list="$OPTARG"
;;
"o")
output_file="$OPTARG"
;;
"p")
ping_host="$OPTARG"
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
exit 1
;;
"q" | *)
quiet_updates=1
;;
"h" | *)
usage
exit 1
;;
esac
done
fi
if [ -z "$site_list" ]; then
echo "Site list file required, use \"-f filename\" or set script variable"
usage
exit 1
fi
if [ ! -r "$site_list" ]; then
echo "Can't read sitefile: $site_list"
exit 1
fi
if [ -z "$output_file" ]; then
echo "Output file required, use \"-o filename\" or set script variable"
usage
exit 1
fi
if [ ! -r "$output_file" ]; then
if [ ! -e "$output_file" ]; then
touch "$output_file"
if [ "$?" -ne 0 ]; then
echo "Can't create output file: $output_file"
exit 1
fi
else
echo "Can't read output file: $output_file"
exit 1
fi
fi
# perform ping test with short timers for connectivity and exit if fails
if [ ! -z "$ping_host" ]; then
$ping_cmd $ping_host &> /dev/null
if [ "$?" != 0 ]; then
echo "Ping test failed: $ping_host"
exit 1
fi
fi
do_sites