Photo by Unsplash
827 字
4 分钟
Linux SOCKS5代理延迟测试脚本
Linux SOCKS5 代理延迟测试脚本
手上有多个代理节点时,想知道哪个连接目标网站最快,手动测试太麻烦。这里分享一个 Bash 脚本,自动批量测试并排序。
基础命令速查
单次测试
最简单的测试方法是用 curl:
curl -o /dev/null -s -w "DNS解析: %{time_namelookup}sTCP连接: %{time_connect}sSSL握手: %{time_appconnect}s首字节: %{time_starttransfer}s总耗时: %{time_total}s" -x socks5h://用户名:密码@IP:端口 https://api.groq.com协议选择使用
socks5h://而不是socks5://,前者会让代理服务器进行 DNS 解析,绕过本地 DNS 污染,结果更准确。
批量测试脚本
如果你有多个节点需要对比,用这个脚本:
#!/bin/bash
# 目标URLTARGET_URL="https://api.groq.com"TIMEOUT=5TEST_COUNT=12
# 节点列表PROXIES=( "HK|socks5h://user:[email protected]:58888" "JP|socks5h://user:[email protected]:58888" "SG|socks5h://user:[email protected]:58888" "US|socks5h://user:[email protected]:58888")
echo "----------------------------------------------------------------"echo "目标: $TARGET_URL"echo "每个节点测 ${TEST_COUNT} 次,去掉最高/最低,取剩余 $(($TEST_COUNT-2)) 次平均"echo "----------------------------------------------------------------"printf "%-15s | %-12s | %-10s\n" "节点" "平均延迟" "失败次数"echo "----------------------------------------------------------------"
RESULTS_FILE=$(mktemp)
for proxy_entry in "${PROXIES[@]}"; do IFS='|' read -r name url <<< "$proxy_entry"
raw_times="" fail_count=0
# 循环测试 for (( i=1; i<=TEST_COUNT; i++ )); do printf "\r%-15s | 测试中... [%2d/%d]" "$name" "$i" "$TEST_COUNT"
output=$(curl -s -w "%{http_code},%{time_total}" -o /dev/null \ --connect-timeout $TIMEOUT -x "$url" "$TARGET_URL")
code=$(echo "$output" | cut -d',' -f1) time=$(echo "$output" | cut -d',' -f2)
# 判断是否有效 (2xx/3xx 且耗时>0) if [[ "$code" =~ ^[234] ]] && (( $(echo "$time > 0" | bc -l 2>/dev/null || echo 0) )); then raw_times="$raw_times $time" else ((fail_count++)) fi done
# 数据处理:去头去尾取平均 sorted_times=$(echo "$raw_times" | tr ' ' '\n' | sort -n | grep -v '^$') valid_count=$(echo "$sorted_times" | grep -c .)
if [ "$valid_count" -ge 3 ]; then # 去掉最小和最大 trimmed_times=$(echo "$sorted_times" | sed '1d;$d') # 计算平均值 avg_time=$(echo "$trimmed_times" | awk '{sum+=$1} END {printf "%.3f", sum/NR}')
printf "\r%-15s | %-10ss | %-10s\n" "$name" "$avg_time" "${fail_count}次" echo "$avg_time $name $fail_count" >> "$RESULTS_FILE" else printf "\r%-15s | %-12s | %-10s\n" "$name" "N/A" "${fail_count}次" fidone
echo "----------------------------------------------------------------"echo "最终排名 (越小越快):"echo "----------------------------------------------------------------"echo -e "排名\t延迟\t失败次数\t节点"sort -n "$RESULTS_FILE" | awk '{printf "No.%d\t%ss\t%s\t%s\n", NR, $1, $3, $2}'
rm -f "$RESULTS_FILE"脚本说明
测试逻辑:
- 每个节点测 12 次
- 去掉 1 个最快(可能命中缓存)和 1 个最慢(网络抖动)
- 剩余 10 次取平均
关键指标:
time_appconnect- SSL 握手时间,反映线路物理质量time_total- 总耗时,包含 DNS、建连、握手、响应
使用方法
chmod +x test_proxies.sh./test_proxies.sh输出示例:
节点 | 平均延迟 | 失败次数----------------------------------------------------------------US | 0.245s | 0次JP | 0.312s | 0次SG | 0.389s | 0次HK | 0.456s | 2次----------------------------------------------------------------最终排名 (越小越快):----------------------------------------------------------------排名 延迟 失败次数 节点No.1 0.245s 0 USNo.2 0.312s 0 JPNo.3 0.389s 0 SGNo.4 0.456s 2 HK其他代理协议
# HTTP 代理curl -x http://127.0.0.1:8080 https://ip.sb
# HTTPS 代理curl -x https://127.0.0.1:8443 https://ip.sb
# SOCKS4 代理curl -x socks4://127.0.0.1:1080 https://ip.sb
# SOCKS4a 代理(远端 DNS)curl -x socks4a://127.0.0.1:1080 https://ip.sb使用环境变量
设置临时环境变量(当前 shell 会话有效):
export ALL_PROXY=socks5h://127.0.0.1:40000curl https://ip.sb
# 取消设置unset ALL_PROXY批量测试脚本
如果你有多个节点需要对比,用这个脚本:
-
节点离线
Terminal window nc -vz IP 端口 -
认证失败 - 用户名或密码错误
-
IP被墙 - 连接被 RST
-
目标网站拒绝 - 某些网站会封代理IP
进阶:持续监控
想持续观察节点稳定性?加个循环:
while true; do ./test_proxies.sh echo "等待60秒后下一轮..." sleep 60done参考链接
Linux SOCKS5代理延迟测试脚本
https://im.awsl.app/posts/networking/037-socks5-latency-test/