forked from deppapparatus/better-cloudflare-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf.sh
285 lines (285 loc) · 7.86 KB
/
cf.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
#!/bin/bash
# random cloudflare anycast ip
declare -i bandwidth
declare -i speed
read -p "请设置期望到 CloudFlare 服务器的带宽大小(单位 Mbps):" bandwidth
speed=bandwidth*128*1024
starttime=`date +'%Y-%m-%d %H:%M:%S'`
while true
do
while true
do
declare -i n
declare -i per
rm -rf icmp temp log.txt anycast.txt
mkdir icmp
curl https://anycast.freecdn.workers.dev -s -# -o anycast.txt
n=0
m=$(cat anycast.txt | wc -l)
for i in `cat anycast.txt`
do
ping -c 60 -i 0.2 -n -q $i > icmp/$n.log&
per=$n*100/$m
n=$[$n+1]
while true
do
p=$(ps -ef | grep ping | grep -v "grep" | wc -l)
if [ $p -ge 200 ]
then
echo 正在测试 ICMP 丢包率:进程数 $p,已完成 $per %
sleep 0.5
else
echo 正在测试 ICMP 丢包率:进程数 $p,已完成 $per %
break
fi
done
done
rm -rf anycast.txt
while true
do
p=$(ps -ef | grep ping | grep -v "grep" | wc -l)
if [ $p -ne 0 ]
then
echo 等待 ICMP 进程结束:剩余进程数 $p
sleep 1
else
echo ICMP 丢包率测试完成
break
fi
done
cat icmp/*.log | sed -n '3~5p;4~5p' | sed -n '{N;s/\n/\t/p}' | cut -f1 -d'%' | awk '{print $2,$NF}' | sort -k 2 -n | awk '{print $1}' | sed '101,$d' > ip.txt
rm -rf icmp
echo 选取100个丢包率最少的IP地址下载测速
mkdir temp
for i in `cat ip.txt`
do
echo $i 启动测速
curl --resolve speed.cloudflare.com:443:$i https://speed.cloudflare.com/__down?bytes=1000000000 -o temp/$i -s --connect-timeout 2 --max-time 10&
sleep 0.5
done
echo 等待测速进程结束,筛选出三个优选的IP
sleep 15
echo 测速完成
ls -S temp > ip.txt
rm -rf temp
n=$(wc -l ip.txt | awk '{print $1}')
if [ $n -ge 3 ]; then
first=$(sed -n '1p' ip.txt)
second=$(sed -n '2p' ip.txt)
third=$(sed -n '3p' ip.txt)
rm -rf ip.txt
echo 优选的IP地址为 $first - $second - $third
echo 第一次测试 $first
curl --resolve speed.cloudflare.com:443:$first https://speed.cloudflare.com/__down?bytes=1000000000 -o /dev/null --connect-timeout 5 --max-time 15 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
max=0
for i in `cat speed.txt`
do
max=$i
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$first
break
fi
max=$[$max/1024]
echo 峰值速度 $max kB/s
echo 第二次测试 $first
curl --resolve speed.cloudflare.com:443:$first https://speed.cloudflare.com/__down?bytes=1000000000 -o /dev/null --connect-timeout 5 --max-time 15 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
max=0
for i in `cat speed.txt`
do
max=$i
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$first
break
fi
max=$[$max/1024]
echo 峰值速度 $max kB/s
echo 第一次测试 $second
curl --resolve speed.cloudflare.com:443:$second https://speed.cloudflare.com/__down?bytes=1000000000 -o /dev/null --connect-timeout 5 --max-time 15 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
max=0
for i in `cat speed.txt`
do
max=$i
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$second
break
fi
max=$[$max/1024]
echo 峰值速度 $max kB/s
echo 第二次测试 $second
curl --resolve speed.cloudflare.com:443:$second https://speed.cloudflare.com/__down?bytes=1000000000 -o /dev/null --connect-timeout 5 --max-time 15 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
max=0
for i in `cat speed.txt`
do
max=$i
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$second
break
fi
max=$[$max/1024]
echo 峰值速度 $max kB/s
echo 第一次测试 $third
curl --resolve speed.cloudflare.com:443:$third https://speed.cloudflare.com/__down?bytes=1000000000 -o /dev/null --connect-timeout 5 --max-time 15 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
max=0
for i in `cat speed.txt`
do
max=$i
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$third
break
fi
max=$[$max/1024]
echo 峰值速度 $max kB/s
echo 第二次测试 $third
curl --resolve speed.cloudflare.com:443:$third https://speed.cloudflare.com/__down?bytes=1000000000 -o /dev/null --connect-timeout 5 --max-time 15 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
max=0
for i in `cat speed.txt`
do
max=$i
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$third
break
fi
max=$[$max/1024]
echo 峰值速度 $max kB/s
fi
done
break
done
max=$[$max/1024]
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s)
end_seconds=$(date --date="$endtime" +%s)
clear
echo $anycast 满足 $bandwidth Mbps带宽需求,峰值速度 $max kB/s,总计用时 $((end_seconds-start_seconds)) 秒