#!/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 # 检查 IP 地址与 Cloudflare 的连通性 status_code=$(curl --resolve cloudflare.com:443:$ip --insecure https://cloudflare.com/cdn-cgi/trace -o /dev/null -w '%{http_code}' -s) # 如果 HTTP 状态码为 200 if [ $status_code -eq 200 ]; 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 connectivity issues with Cloudflare" fi 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=100000000 -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; (100000000*8)/(($elapsed_time)*1000000)" | 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