添加 choose-cn.sh

This commit is contained in:
chunzhi 2024-01-22 21:34:32 +08:00
parent fc11a91e23
commit 792cc6cac3

60
choose-cn.sh Normal file
View File

@ -0,0 +1,60 @@
#!/bin/bash
# 使用 dig 命令查询 DNS 记录并写入 ip.txt
dig cf.youoffer.net A +short > ip.txt
# 创建一个新的文件 ip_use.txt
touch ip_use.txt
# 读取 ip.txt 中的每个 IP 地址
while IFS= read -r ip
do
# 使用 ping 命令检查延迟
ping_result=$(ping -c 1 -W 1 "$ip" | grep ttl)
# 如果 ping 成功
if [ $? -eq 0 ]; then
# 提取延迟时间
ping_time=$(echo "$ping_result" | cut -d'=' -f 4 | cut -d' ' -f 1)
# 如果延迟时间小于 150ms
if (( $(echo "$ping_time < 150" | bc -l) )); then
echo "Ping to $ip successful with a latency of $ping_time ms"
# 将小于 150ms 的 IP 写入 ip_use.txt
echo "$ip" >> ip_use.txt
else
echo "Ping to $ip discarded due to high latency of $ping_time ms"
fi
else
echo "Ping to $ip failed"
fi
done < ip.txt
# 读取 ip_use.txt 中的每个 IP 地址进行下载测试
while IFS= read -r ip
do
echo "Starting download test for $ip"
# 使用 curl 命令进行下载测试
start_time=$(date +%s.%N)
curl -s --resolve speed.cloudflare.com:443:"$ip" https://speed.cloudflare.com/__down?bytes=10000000 -o /dev/null --connect-timeout 5
curl_status=$?
end_time=$(date +%s.%N)
elapsed_time=$(echo "$end_time - $start_time" | bc)
# 检查 curl 命令的返回状态
if [ $curl_status -eq 0 ]; then
# 计算下载速度
download_speed=$(echo "scale=2; (10000000*8)/(($elapsed_time)*100000)" | bc)
# 如果下载速度小于 50Mbps
if (( $(echo "$download_speed < 50" | bc -l) )); then
# 从 ip_use.txt 删除下载速度小于 50Mbps 的 IP
sed -i "/$ip/d" ip_use.txt
else
echo "$ip has a download speed of $download_speed Mbps"
fi
else
echo "Download test for $ip failed with status $curl_status"
fi
echo "Finished download test for $ip"
done < ip_use.txt