-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDNS-control
executable file
·269 lines (215 loc) · 5.55 KB
/
DDNS-control
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
# define directories
origin=`cd $(dirname $0);pwd `
state_dir="$origin/.state"
exec_dir="$origin/exec"
service_dir="$origin/service"
working_dir="/etc/ddns"
system_service="/etc/systemd/system"
# tools part
Usage() {
echo "Usage: $0 { config | clean | start | stop | status } [conf] | list | update [conf] [min]"
echo "[conf] : the name of your ddns configuration(without '.conf')."
exit 1
}
Parameter_judge() {
correct_num=$1
real_num=$2
if [ $correct_num != $real_num ];then
Usage
fi
}
Print_RED() {
RED='\033[0;31m'
NC='\033[0m'
echo -e "${RED}$1${NC}"
}
Get_PID() {
conf="$1"
# get the process ID of conf
PID=`sudo ps aux | grep noip | grep $conf | awk -F ' ' '{print $2}'`
if [ "$PID" = "" ];then
PID="No processes active"
fi
echo $PID
}
Check_state() {
conf="$1"
correct_state="$2"
current_action="$3"
# if state file exists ,read its state.
# if not, state is init
if [ ! -f "$state_dir/$conf.state" ];then
current_state="init"
else
current_state=`cat $state_dir/$conf.state`
fi
# if state is not correct, warning and exit
if [ "$current_state" != "$correct_state" ];then
Print_RED "Error:"
echo "The $conf state is $current_state."
echo "You can not do $current_action on this state."
echo "The state must be $correct_state."
exit 1
fi
}
Change_state() {
conf="$1"
state="$2"
echo "$state" > $state_dir/$conf.state
}
Get_status() {
conf="$1"
state=`cat $state_dir/$conf.state`
Print_RED "Conf: $conf"
Print_RED "State: $state"
Print_RED "Service status"
sudo systemctl status ddns-$conf.timer
sudo systemctl status ddns-$conf.service
Print_RED "DDNS status"
conf_file=`ls /etc/ddns | grep $conf.conf`
if [ "$conf_file" = "" ];then
echo "No configuration named $conf"
exit 1
fi
conf=${conf_file:0:(-5)}
PID=`Get_PID $conf`
echo "PID: $PID"
cd $working_dir
sudo ./noip2-x86_64 -S -c $conf.conf 2>&1 | tail -n 5
}
# main part
Config() {
conf="$1"
# check state
Check_state $conf init config
# initiallize state
touch $state_dir/$conf.state
Change_state $conf init
# copy noip exec to /etc/ddns and generate noip config
sudo mkdir -p $working_dir
sudo cp $exec_dir/noip2-x86_64 $working_dir/
sudo $working_dir/noip2-x86_64 -C -c $working_dir/$conf.conf
# judge if generate correct
if [ ! -f "$working_dir/$conf.conf" ];then
Print_RED "Error:"
echo "Config failed! Please try again!"
exit 1
fi
# config service execuable script
sudo cp $exec_dir/ddns.sh.template $working_dir/ddns-$conf.sh
sudo sed -i "s/NOIP-CONF/$conf/g" $working_dir/ddns-$conf.sh
sudo chmod +x $working_dir/ddns-$conf.sh
# config service
sudo cp $service_dir/ddns.service.template $system_service/ddns-$conf.service
sudo sed -i "s/CONF/$conf/g" $system_service/ddns-$conf.service
# config timer
sudo cp $service_dir/ddns.timer.template $system_service/ddns-$conf.timer
sudo sed -i "s/CONF/$conf/g" $system_service/ddns-$conf.timer
# change state
Change_state $conf "configured"
# show up status
Get_status $conf
}
Start() {
conf="$1"
# check state
Check_state $conf configured start
# start ddns service
Print_RED "start ddns $conf"
sudo systemctl start ddns-$conf.service
sudo systemctl start ddns-$conf.timer
sudo systemctl enable ddns-$conf.timer
# change state
Change_state $conf "running"
# show up status
Get_status $conf
}
Stop() {
conf="$1"
# check state
Check_state $conf running stop
# stop ddns service
Print_RED "stop ddns $conf"
for cmd in disable stop
do
sudo systemctl $cmd ddns-$conf.timer
sudo systemctl $cmd ddns-$conf.service
done
# change state log
Change_state $conf "configured"
# show up status
Get_status $conf
}
Clean() {
conf="$1"
# check state
Check_state $conf configured clean
# remove service configure
Print_RED "clean the configure of $conf"
sudo rm $system_service/ddns-$conf.service
sudo rm $system_service/ddns-$conf.timer
# remove configure
sudo rm $working_dir/$conf.conf
sudo rm $working_dir/ddns-$conf.sh
# remove /etc/ddns when there is no configure in it
ls /etc/ddns/ | grep ddns > /dev/null 2>&1 || sudo rm -r /etc/ddns/
# remove auto generate files "NO-IP*"
rm $origin/NO-IP* > /dev/null 2>&1
# change state
rm $state_dir/$conf.state
# show up state
Get_status $conf
}
List() {
conf_list=`ls /etc/ddns | grep .conf`
for file in $conf_list
do
conf=${file:0:(-5)}
Get_status $conf
done
}
Update() {
conf="$1"
min="$2"
# check state
Check_state $conf running update
# update minute
sudo $working_dir/noip2-x86_64 -U $min -c $working_dir/$conf.conf
}
# case part
case "$1" in
config)
Parameter_judge 2 $#
Config $2
;;
clean)
Parameter_judge 2 $#
Clean $2
;;
start)
Parameter_judge 2 $#
Start $2
;;
stop)
Parameter_judge 2 $#
Stop $2
;;
status)
Parameter_judge 2 $#
Get_status $2
;;
list)
Parameter_judge 1 $#
List
;;
update)
Parameter_judge 3 $#
Update $2 $3
Stop $2
Start $2
;;
*)
Usage
;;
esac