From ec956e6b22c5d0ac2fed7c0580d1fc32b9202ba7 Mon Sep 17 00:00:00 2001 From: chunzhi Date: Thu, 8 Feb 2024 19:05:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20choose-cn-v2.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入延迟检测 --- choose-cn-v2.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/choose-cn-v2.sh b/choose-cn-v2.sh index df0cd22..d67525c 100644 --- a/choose-cn-v2.sh +++ b/choose-cn-v2.sh @@ -23,6 +23,35 @@ dig cf.youoffer.net A +short > ip.txt # 创建一个新的文件 final_ips.txt touch final_ips.txt +#延迟检测部分 +while IFS= read -r ip +do + echo "Checking latency for $ip" + # 使用 ping 命令检查延迟 + ping_result=$(ping -c 1 -W 1 "$ip" 2>&1) + ping_status=$? + + # 如果 ping 成功 + if [ $ping_status -eq 0 ]; then + # 提取延迟时间 + ping_time=$(echo "$ping_result" | grep time= | cut -d'=' -f 4 | cut -d' ' -f 1) + + # 显示延迟时间 + echo "Latency for $ip is $ping_time ms" + + # 如果延迟时间大于 200ms + if (( $(echo "$ping_time > 200" | bc -l) )); then + echo "Latency for $ip is too high, discarding." + # 从 ip.txt 删除延迟时间大于 200ms 的 IP + sed -i "/$ip/d" ip.txt + fi + else + echo "Ping to $ip failed or timed out." + # 从 ip.txt 删除无法 ping 通的 IP + sed -i "/$ip/d" ip.txt + fi +done < ip.txt + # 读取 ip.txt 中的每个 IP 地址进行下载测试 while IFS= read -r ip do