-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddns.sh
308 lines (289 loc) · 9.48 KB
/
ddns.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cat <<"EOF"
_
___ ___ _ __ __ _ ___| |__ _ _ __ _____
/ __|/ _ \| '_ \ / _` / __| '_ \| | | | \ \ /\ / / _ \
\__ \ (_) | | | | (_| \__ \ | | | |_| | \ V V / (_) |
|___/\___/|_| |_|\__, |___/_| |_|\__,_| \_/\_/ \___/
|___/
Author: songshu wo
EOF
check_sys() {
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif cat /etc/issue | grep -q -E -i "debian"; then
release="debian"
elif cat /etc/issue | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
elif cat /proc/version | grep -q -E -i "debian"; then
release="debian"
elif cat /proc/version | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
fi
}
welcome() {
echo ""
echo "欢迎使用 ddns gandi。"
echo "安装即将开始"
echo "如果您想取消安装,"
echo "请在 3 秒钟内按 Ctrl+C 终止此脚本。"
echo ""
sleep 3
}
yum_update() {
echo "正在优化 yum . . ."
echo "此过程稍慢 因为需要升级系统依赖"
yum update -y >>/dev/null 2>&1
}
yum_git_check() {
echo "正在检查 Git 安装情况 . . ."
if command -v git >>/dev/null 2>&1; then
echo "Git 似乎存在,安装过程继续 . . ."
else
echo "Git 未安装在此系统上,正在进行安装"
yum install git -y >>/dev/null 2>&1
fi
}
yum_python_check() {
echo "正在检查 python 安装情况 . . ."
if command -v python3 >>/dev/null 2>&1; then
U_V1=$(python3 -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1}')
U_V2=$(python3 -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $2}')
if [ $U_V1 -gt 3 ]; then
echo 'Python 3.6+ 存在 . . .'
elif [ $U_V2 -ge 6 ]; then
echo 'Python 3.6+ 存在 . . .'
PYV=$U_V1.$U_V2
PYV=$(which python$PYV)
else
if command -v python3.6 >>/dev/null 2>&1; then
echo 'Python 3.6+ 存在 . . .'
PYV=$(which python3.6)
else
echo "Python3.6 未安装在此系统上,正在进行安装"
yum install python3 -y >>/dev/null 2>&1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 >>/dev/null 2>&1
PYV=$(which python3.6)
fi
fi
else
echo "Python3.6 未安装在此系统上,正在进行安装"
yum install python3 -y >>/dev/null 2>&1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 >>/dev/null 2>&1
fi
if command -v pip3 >>/dev/null 2>&1; then
echo 'pip 存在 . . .'
else
echo "pip3 未安装在此系统上,正在进行安装"
yum install -y python3-pip >>/dev/null 2>&1
fi
}
apt_update() {
echo "正在优化 apt-get . . ."
apt-get install sudo cron -y >>/dev/null 2>&1
apt-get update >>/dev/null 2>&1
}
apt_git_check() {
echo "正在检查 Git 安装情况 . . ."
if command -v git >>/dev/null 2>&1; then
echo "Git 似乎存在, 安装过程继续 . . ."
else
echo "Git 未安装在此系统上,正在进行安装"
apt-get install git -y >>/dev/null 2>&1
fi
}
apt_python_check() {
echo "正在检查 python 安装情况 . . ."
if command -v python3 >>/dev/null 2>&1; then
U_V1=$(python3 -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1}')
U_V2=$(python3 -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $2}')
if [ $U_V1 -gt 3 ]; then
echo 'Python 3.6+ 存在 . . .'
elif [ $U_V2 -ge 6 ]; then
echo 'Python 3.6+ 存在 . . .'
PYV=$U_V1.$U_V2
PYV=$(which python$PYV)
else
if command -v python3.6 >>/dev/null 2>&1; then
echo 'Python 3.6+ 存在 . . .'
PYV=$(which python3.6)
else
echo "Python3 未安装在此系统上,正在进行安装"
add-apt-repository ppa:deadsnakes/ppa -y
apt-get update >>/dev/null 2>&1
apt-get install python3 -y >>/dev/null 2>&1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3 1 >>/dev/null 2>&1
PYV=$(which python3.6)
fi
fi
else
echo "Python3.6 未安装在此系统上,正在进行安装"
add-apt-repository ppa:deadsnakes/ppa -y
apt-get update >>/dev/null 2>&1
apt-get install python3 -y >>/dev/null 2>&1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3 1 >>/dev/null 2>&1
fi
if command -v pip3 >>/dev/null 2>&1; then
echo 'pip 存在 . . .'
else
echo "pip3 未安装在此系统上,正在进行安装"
apt-get install -y python3-pip >>/dev/null 2>&1
fi
}
debian_python_check() {
echo "正在检查 python 安装情况 . . ."
if command -v python3 >>/dev/null 2>&1; then
U_V1=$(python3 -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1}')
U_V2=$(python3 -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $2}')
if [ $U_V1 -gt 3 ]; then
echo 'Python 3.6+ 存在 . . .'
elif [ $U_V2 -ge 6 ]; then
echo 'Python 3.6+ 存在 . . .'
PYV=$U_V1.$U_V2
PYV=$(which python$PYV)
else
if command -v python3.6 >>/dev/null 2>&1; then
echo 'Python 3.6+ 存在 . . .'
PYV=$(which python3.6)
else
echo "Python3.6 未安装在此系统上,正在进行安装"
apt-get update >>/dev/null 2>&1
apt-get install python3 -y >>/dev/null 2>&1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3 1 >>/dev/null 2>&1
PYV=$(which python3.6)
fi
fi
else
echo "Python3.6 未安装在此系统上,正在进行安装"
apt-get update >>/dev/null 2>&1
apt-get install python3 cron -y >>/dev/null 2>&1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3 1 >>/dev/null 2>&1
fi
echo "正在检查 pip3 安装情况 . . ."
if command -v pip3 >>/dev/null 2>&1; then
echo 'pip 存在 . . .'
else
echo "pip3 未安装在此系统上,正在进行安装"
apt-get install -y python3-pip >>/dev/null 2>&1
fi
}
download_repo() {
echo "下载 repository 中 . . ."
cd /root >>/dev/null 2>&1
git clone https://github.com/shzxm/gandi-ddns.git
cd /root/gandi-ddns
echo "Hello World!" >/root/gandi-ddns/.lock
}
pypi_install() {
echo "下载安装 pypi 依赖中 . . ."
$PYV -m pip install --upgrade pip >>/dev/null 2>&1
$PYV -m pip install -r requirements.txt >>/dev/null 2>&1
}
configure() {
cp config-template.txt config.txt
echo -n "Please enter apikey:"
read apikey
echo "Writting apikey..."
sed -i -e "s/apikey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/apikey = ${apikey}/g" config.txt
echo "Please enter domain:"
read domain
echo "Writting domain..."
sed -i -e "s/domain = example.com/domain = ${domain}/g" config.txt
echo "Please enter a_name:"
read a_name
echo "Writting a_name..."
sed -i -e "s/a_name = raspbian/a_name = ${a_name}/g" config.txt
cat /root/gandi-ddns/config.txt
echo "Writting system config..."
echo "@reboot python3 /root/gandi-ddns/gandi_ddns.py &" >>/var/spool/cron/root
echo "*/5 * * * * python3 /root/gandi-ddns/gandi_ddns.py" >>/var/spool/cron/root
chmod +x /root/gandi-ddns/gandi_ddns.py
service crond restart
python3 /root/gandi-ddns/gandi_ddns.py
}
install_require() {
if [ "$release" = "centos" ]; then
echo "系统检测通过。"
yum_update
yum_git_check
yum_python_check
pypi_install
elif [ "$release" = "ubuntu" ]; then
echo "系统检测通过。"
apt_update
apt_git_check
apt_python_check
pypi_install
elif [ "$release" = "debian" ]; then
echo "系统检测通过。"
welcome
apt_update
apt_git_check
debian_python_check
pypi_install
else
echo "目前暂时不支持此系统。"
fi
exit 1
}
start_installation() {
if [ "$release" = "centos" ]; then
echo "系统检测通过。"
welcome
yum_update
yum_git_check
yum_python_check
download_repo
pypi_install
configure
echo "ddns 已经安装完毕"
elif [ "$release" = "ubuntu" ]; then
echo "系统检测通过。"
welcome
apt_update
apt_git_check
apt_python_check
download_repo
pypi_install
configure
echo "ddns 已经安装完毕"
elif [ "$release" = "debian" ]; then
echo "系统检测通过。"
welcome
apt_update
apt_git_check
debian_python_check
download_repo
pypi_install
configure
echo "ddns 已经安装完毕"
else
echo "目前暂时不支持此系统。"
fi
exit 1
}
shon_online() {
echo "请选择您需要进行的操作:"
echo " 1) 安装 ddns"
echo " 2) 重新安装 ddns 依赖"
echo " 3) 退出脚本"
echo ""
echo " Version:0.1"
echo ""
echo -n "请输入编号: "
read N
case $N in
1) start_installation ;;
2) install_require ;;
3) exit ;;
*) echo "Wrong input!" ;;
esac
}
check_sys
shon_online