-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtestspeed.sh
More file actions
286 lines (235 loc) · 10.6 KB
/
testspeed.sh
File metadata and controls
286 lines (235 loc) · 10.6 KB
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
#!/bin/bash
Green="\033[32m" && Yellow="\033[33m" && Red="\033[31m" && Blue="\033[36m" && Font_suffix="\033[0m"
Info="${Green}[Info]${Font_suffix}"
Error="${Red}[Error]${Font_suffix}"
echo -e "${Green}
#======================================
# Project: testspeed
# Version: 1.4.0
# Author: nanqinlang
# Blog: https://sometimesnaive.org
# Github: https://github.com/nanqinlang
#======================================${Font_suffix}"
check_root(){
[[ "`id -u`" != "0" ]] && echo -e "${Error} must be root user" && exit 1
}
directory(){
[[ ! -d /home/testspeed ]] && mkdir -p /home/testspeed
cd /home/testspeed
}
select_ISP(){
echo -e "${Info} 选择需要测速的目标网络: \n1.中国电信\n2.中国联通\n3.中国移动"
read -p "输入数字以选择:" ISP
while [[ ! "${ISP}" =~ ^[1-3]$ ]]
do
echo -e "${Error} 无效输入"
echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" ISP
done
}
set_ISP_name(){
[[ "${ISP}" == "1" ]] && ISP_name="电信"
[[ "${ISP}" == "2" ]] && ISP_name="联通"
[[ "${ISP}" == "3" ]] && ISP_name="移动"
}
select_position(){
echo -e "${Info} 选择你的方位: \n1.北方\n2.南方"
read -p "输入数字以选择:" position
while [[ ! "${position}" =~ ^[1-2]$ ]]
do
echo -e "${Error} 无效输入"
echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" position
done
}
select_city(){
echo -e "${Info} 选择最靠近你的城市:"
[[ "${ISP}" == "1" && "${position}" == "1" ]] && city_11
[[ "${ISP}" == "1" && "${position}" == "2" ]] && city_12
[[ "${ISP}" == "2" && "${position}" == "1" ]] && city_21
[[ "${ISP}" == "2" && "${position}" == "2" ]] && city_22
[[ "${ISP}" == "3" && "${position}" == "1" ]] && city_31
[[ "${ISP}" == "3" && "${position}" == "2" ]] && city_32
}
city_11(){
echo -e "1.北京1\n2.北京2\n3.天津\n4.上海\n5.甘肃兰州\n6.河南郑州" && read -p "输入数字以选择:" city
# while [[ ! "${city}" =~ ^[1-6]$ ]]
# do
# echo -e "${Error} 无效输入"
# echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" city
# done
}
city_12(){
echo -e "1.湖北襄阳\n2.湖南长沙\n3.四川成都\n4.安徽合肥\n5.江西南昌1\n6.江西南昌2\n7.江苏苏州\n8.江苏南京\n9.云南昆明" && read -p "输入数字以选择:" city
# while [[ ! "${city}" =~ ^[1-9]$ ]]
# do
# echo -e "${Error} 无效输入"
# echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" city
# done
}
city_21(){
echo -e "1.辽宁沈阳\n2.上海\n3.北京\n4.天津\n5.重庆\n6.甘肃兰州\n7.河南郑州\n8.乌鲁木齐" && read -p "输入数字以选择:" city
# while [[ ! "${city}" =~ ^[1-8]$ ]]
# do
# echo -e "${Error} 无效输入"
# echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" city
# done
}
city_22(){
echo -e "1.湖北武汉\n2.安徽合肥\n3.江西南昌\n4.浙江杭州\n5.广西南宁\n6.海南海口" && read -p "输入数字以选择:" city
# while [[ ! "${city}" =~ ^[1-6]$ ]]
# do
# echo -e "${Error} 无效输入"
# echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" city
# done
}
city_31(){
echo -e "1.上海" && read -p "输入数字以选择:" city
# while [[ "${city}" != "1" ]]
# do
# echo -e "${Error} 无效输入"
# echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" city
# done
}
city_32(){
echo -e "1.四川成都\n2.安徽合肥\n3.江苏苏州\n4.江苏无锡\n5.浙江宁波\n6.云南昆明" && read -p "输入数字以选择:" city
# while [[ ! "${city}" =~ ^[1-6]$ ]]
# do
# echo -e "${Error} 无效输入"
# echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" city
# done
}
set_id(){
[[ "${ISP}" == "1" && "${position}" == "1" && "${city}" == "1" ]] && id=4751 && city_name="北京1"
[[ "${ISP}" == "1" && "${position}" == "1" && "${city}" == "2" ]] && id=4589 && city_name="北京2"
[[ "${ISP}" == "1" && "${position}" == "1" && "${city}" == "3" ]] && id=6714 && city_name="天津"
[[ "${ISP}" == "1" && "${position}" == "1" && "${city}" == "4" ]] && id=3633 && city_name="上海"
[[ "${ISP}" == "1" && "${position}" == "1" && "${city}" == "5" ]] && id=3973 && city_name="甘肃兰州"
[[ "${ISP}" == "1" && "${position}" == "1" && "${city}" == "6" ]] && id=4595 && city_name="河南郑州"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "1" ]] && id=6435 && city_name="湖北襄阳"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "2" ]] && id=6132 && city_name="湖南长沙"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "3" ]] && id=4624 && city_name="四川成都"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "4" ]] && id=4433 && city_name="安徽合肥"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "5" ]] && id=6345 && city_name="江西南昌1"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "6" ]] && id=7643 && city_name="江西南昌2"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "7" ]] && id=5274 && city_name="江苏苏州"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "8" ]] && id=5316 && city_name="江苏南京"
[[ "${ISP}" == "1" && "${position}" == "2" && "${city}" == "9" ]] && id=6415 && city_name="云南昆明"
[[ "${ISP}" == "2" && "${position}" == "1" && "${city}" == "1" ]] && id=5017 && city_name="辽宁沈阳"
[[ "${ISP}" == "2" && "${position}" == "1" && "${city}" == "2" ]] && id=5083 && city_name="上海"
[[ "${ISP}" == "2" && "${position}" == "1" && "${city}" == "3" ]] && id=5145 && city_name="北京"
[[ "${ISP}" == "2" && "${position}" == "1" && "${city}" == "4" ]] && id=5475 && city_name="天津"
[[ "${ISP}" == "2" && "${position}" == "1" && "${city}" == "5" ]] && id=5726 && city_name="重庆"
[[ "${ISP}" == "2" && "${position}" == "1" && "${city}" == "6" ]] && id=4690 && city_name="甘肃兰州"
[[ "${ISP}" == "2" && "${position}" == "1" && "${city}" == "7" ]] && id=6810 && city_name="河南郑州"
[[ "${ISP}" == "2" && "${position}" == "1" && "${city}" == "8" ]] && id=6144 && city_name="乌鲁木齐"
[[ "${ISP}" == "2" && "${position}" == "2" && "${city}" == "1" ]] && id=5485 && city_name="湖北武汉"
[[ "${ISP}" == "2" && "${position}" == "2" && "${city}" == "2" ]] && id=5724 && city_name="安徽合肥"
[[ "${ISP}" == "2" && "${position}" == "2" && "${city}" == "3" ]] && id=7230 && city_name="江西南昌"
[[ "${ISP}" == "2" && "${position}" == "2" && "${city}" == "4" ]] && id=5300 && city_name="浙江杭州"
[[ "${ISP}" == "2" && "${position}" == "2" && "${city}" == "5" ]] && id=5674 && city_name="广西南宁"
[[ "${ISP}" == "2" && "${position}" == "2" && "${city}" == "6" ]] && id=5985 && city_name="海南海口"
[[ "${ISP}" == "3" && "${position}" == "1" && "${city}" == "1" ]] && id=4665 && city_name="上海"
[[ "${ISP}" == "3" && "${position}" == "2" && "${city}" == "1" ]] && id=4575 && city_name="四川成都"
[[ "${ISP}" == "3" && "${position}" == "2" && "${city}" == "2" ]] && id=4377 && city_name="安徽合肥"
[[ "${ISP}" == "3" && "${position}" == "2" && "${city}" == "3" ]] && id=3927 && city_name="江苏苏州"
[[ "${ISP}" == "3" && "${position}" == "2" && "${city}" == "4" ]] && id=5122 && city_name="江苏无锡"
[[ "${ISP}" == "3" && "${position}" == "2" && "${city}" == "5" ]] && id=6715 && city_name="浙江宁波"
[[ "${ISP}" == "3" && "${position}" == "2" && "${city}" == "6" ]] && id=5892 && city_name="云南昆明"
}
install(){
[[ ! -f speedtest.py ]] && wget https://raw.githubusercontent.com/nanqinlang/testspeed/master/speedtest.py
[[ ! -f speedtest.py ]] && echo -e "${Error} download failed, please check!" && exit 1
chmod 7777 speedtest.py
}
info(){
upload=`echo "${testinfo}" | awk -F ':' '/Upload/{print $2}'`
latency=`echo "${testinfo}" | awk -F ':' '/Hosted/{print $2}'`
date=`date +%Y-%m-%d" "%H:%M:%S`
}
test_single(){
select_ISP
set_ISP_name
select_position
select_city
set_id
ISP_single
}
ISP_single(){
testinfo=`python speedtest.py --server ${id}`
download=`echo "${testinfo}" | awk -F ':' '/Download/{print $2}'`
if [[ -n "${download}" ]]; then
info && hostedby=`echo "${testinfo}" | awk -F ':' '/Hosted/{print $1}'`
result_single | tee -a -i testspeed.log 2>/dev/null
else
echo -e "${Error} 测试失败,可能当前选择节点不可用,请更换其他节点,或稍后再测试该节点"
fi
repeat_test_single
}
result_single(){
echo -e "${Info} ${hostedby}"
echo -e "${Info} 上传 : ${upload}"
echo -e "${Info} 下载 : ${download}"
echo -e "${Info} 延迟 : ${latency}"
echo -e "${Info} 测试时间 : ${date}"
echo -e "${Info} 测试速度 到 ${city_name}${ISP_name} 完成"
}
repeat_test_single(){
echo -e "${Info} 是否继续测试其他节点?"
echo -e "1.是\n2.否"
read -p "请选择:" whether_repeat_single
while [[ ! "${whether_repeat_single}" =~ ^[1-2]$ ]]
do
echo -e "${Error} 无效输入"
echo -e "${Info} 请重新输入" && read -p "请选择:" whether_repeat_single
done
[[ "${whether_repeat_single}" == "1" ]] && test_single
[[ "${whether_repeat_single}" == "2" ]] && echo -e "${Info} 退出脚本 ..." && exit 0
}
test_all(){
ISP_all | tee -a -i testspeed.log 2>/dev/null
}
ISP_all(){
echo -e "${Info} 开始 三网快速测试 ..."
printf "%-14s%-18s%-20s%-12s\n" "ISP Name" "Upload Speed" "Download Speed" "Latency"
result_all '4589' '北京电信'
result_all '4595' '郑州电信'
result_all '4624' '成都电信'
result_all '5081' '深圳电信'
result_all '5017' '沈阳联通'
result_all '5145' '北京联通'
result_all '5724' '合肥联通'
result_all '5674' '南宁联通'
result_all '4377' '合肥移动'
result_all '4575' '成都移动'
result_all '5122' '无锡移动'
result_all '6715' '宁波移动'
echo -e "${Info} 测试时间: ${date}"
echo -e "${Info} 三网测试完成 !"
}
result_all(){
testinfo=`python speedtest.py --server $1`
ISP_name=$2
download=`echo "${testinfo}" | awk -F ':' '/Download/{print $2}'`
if [[ -n "${download}" ]]; then
info
printf "%-17s%-18s%-20s%-12s\n" "${ISP_name}" "${upload}" "${download}" "${latency}"
else
echo -e "${Error} ${ISP_name} 测试失败,可能是此节点现不可用"
echo -e "${Info} 继续测试下一节点 ..."
fi
}
echo -e "${Info} 选择你要使用的功能: "
echo -e "1.指定一个节点测试\n2.三网快速测试"
read -p "输入数字以选择:" function
while [[ ! "${function}" =~ ^[1-2]$ ]]
do
echo -e "${Error} 缺少或无效输入"
echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" function
done
check_root
directory
install
if [[ "${function}" == "1" ]]; then
test_single
else
test_all
fi