From 571f12a8d7346aa3f684f2305f7e1c739301b0d6 Mon Sep 17 00:00:00 2001 From: chunzhi Date: Thu, 8 Feb 2024 14:00:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20choose-cn.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- choose-cn.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 choose-cn.sh diff --git a/choose-cn.sh b/choose-cn.sh new file mode 100644 index 0000000..96f1ee1 --- /dev/null +++ b/choose-cn.sh @@ -0,0 +1,41 @@ +#!/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 + 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 --max-time 10 + 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)*1000000)" | bc) + + # 显示下载速度 + echo "$ip has a download speed of $download_speed Mbps" + + # 如果下载速度大于等于 50Mbps + if (( $(echo "$download_speed >= 50" | bc -l) )); then + # 将大于等于 50Mbps 的 IP 写入 ip_use.txt + echo "$ip" >> ip_use.txt + fi + else + echo "Download test for $ip failed with status $curl_status" + fi +done < ip.txt + +# 重命名 ip_use.txt 为 final_ips.txt,因为 ip_use.txt 在循环中被touch过,可能包含旧数据 +mv ip_use.txt final_ips.txt + +echo "Download tests completed. IPs with download speed >= 50Mbps are written to final_ips.txt"