-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynup
executable file
·146 lines (113 loc) · 2.73 KB
/
dynup
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
#!/bin/bash
#================================================================================
#
#
# FreeDNS update script for useless modems that cannot do the same thing.
#
# Reed Bell - rbell11235@hotmail.co.nz
#
#
#=================================================================================
# Defaults
daemonize=false
freq=600
log_file="/dev/stdout"
query=
verbose=false
# Functions
usage() { echo -e "\nUsage: dynup [-d|-h][-f <int>][-l <str>][-q <str>] \n"
echo -e "-d Detach process and run in background."
echo -e "-f Update frequency (seconds)."
echo -e "-h Show this help message."
echo -e "-l Log updates to a file."
echo -e "-q FreeDNS update url."
echo -e "-v Increase verbosity.\n\n"
echo -e "NOTE:\t(-d) flag is not recommended for most long term setups.\n\tSee README.md to run 'dynup' with systemd."
exit 0
}
update_log(){
echo -e $(date +"%x||%X")"\t$1" >> "$2"
}
call(){
wget -qO- https://checkip.amazonaws.com/
}
update_dns(){
wget --no-check-certificate -qO- $1
}
monitor(){
update_log "Attempting initial update" "$log_file"
base=$(call)
response=$(update_dns $query | sed 's/ to \([0-9]\{1,3\}.\)\{3,3\}\([0-9]\{1,3\}\)//')
if [[ $response =~ "Updated" ]]
then
msg=$response
else
msg="Initial test complete: IP address maintained"
fi
update_log "$msg" "$log_file"
update_log "Now checking every ${freq}s." "$log_file"
while true;
do
sleep $freq
check=$(call)
if [[ $check != $base ]]
then
response=$(update_dns "$query")
if [[ $response =~ "ERROR" ]]
then
msg=$(printf "Error attempting change in DNS assignment. Original assignment %s maintained\n" $base)
update_log "$msg" "$log_file"
else
base=$check
msg=$(printf "DNS updated to %s\n" $check)
update_log "$msg" "$log_file"
fi
else
msg=$(printf "No external IP change detected. Current assignment maintained.\n")
[ $verbose == true ] && update_log "$msg" "$log_file"
fi
done
}
filter_args() {
if [[ $1 -eq 0 ]]; then
usage
exit 0
elif [[ -z "$query" ]]; then
echo -e "Error: mandatory -q flag missing"
usage
exit 1
elif [[ -z "$log_file" ]]; then
echo -e "Error: no log filename was supplied"
usage
exit 1
fi
}
# Parse arguments
while getopts "q:f:hdvl:" opt; do
case ${opt} in
d ) daemonize=true
;;
f ) freq=$OPTARG
;;
h ) usage
;;
l ) log_file=$OPTARG
;;
q ) query=$OPTARG
;;
v ) verbose=true
;;
\? ) usage
;;
esac
done
filter_args $#
# Runtime
if $daemonize; then
update_log "Started dynup monitor script [Update Frequency = ${freq}s]. Now detaching." $log_file
(monitor;) 0<&- &> /dev/null &
disown
else
update_log "Started dynup monitor script [Update Frequency = ${freq}s]" $log_file
monitor
fi