Debian 镜像源测速脚本
声明:此脚本及其说明文档由 Kimi AI 生成,内容仅供参考。
此脚本会测试各个镜像的连接质量、响应延迟和下载速度,并给出最优推荐。
#!/bin/bash
#
# Debian 镜像源测速脚本
# 测试国内主要Debian镜像源的连接质量,选出最优源
#
# ==================== 配置区 ====================
# 定义要测试的镜像源
# 格式:名称|URL|类型
MIRRORS=(
"清华大学|https://mirrors.tuna.tsinghua.edu.cn/debian|教育网"
"中科大|https://mirrors.ustc.edu.cn/debian|教育网"
"CERNET官方|https://mirrors.cernet.edu.cn/debian|教育网"
"上海交通大学|https://mirror.sjtu.edu.cn/debian|教育网"
"腾讯云|http://mirrors.tencent.com/debian|企业"
"阿里云|https://mirrors.aliyun.com/debian|企业"
"网易|http://mirrors.163.com/debian|企业"
"搜狐|http://mirrors.sohu.com/debian|企业"
"兰州大学|http://mirror.lzu.edu.cn/debian|教育网"
"哈尔滨工业大学|http://run.hit.edu.cn/debian|教育网"
"北京理工大学|http://mirror.bit.edu.cn/debian|教育网"
"大连理工大学|http://mirror.dlut.edu.cn/debian|教育网"
"北京交通大学|http://mirror.bjtu.edu.cn/debian|教育网"
"厦门大学|http://mirrors.xmu.edu.cn/debian|教育网"
)
# 测试文件路径(选择较小的Release文件)
TEST_FILE="dists/bookworm/main/binary-amd64/Release"
# Debian 11用户可以改为:dists/bullseye/main/binary-amd64/Release
# 超时时间(秒)
TIMEOUT=10
# 测试次数
TEST_COUNT=3
# 颜色输出
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[36m'
NC='\033[0m' # No Color
# 是否使用IPv6(自动检测)
if ip a | grep -q "inet6.*global"; then
IPV6_SUPPORT=true
echo -e "${BLUE}检测到你的网络支持IPv6${NC}"
else
IPV6_SUPPORT=false
echo -e "${YELLOW}你的网络不支持IPv6,将仅测试IPv4${NC}"
fi
# ==================== 函数区 ====================
# 打印标题
print_header() {
echo "=========================================="
echo " Debian 镜像源测速脚本"
echo " 测试时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "=========================================="
echo ""
}
# 测试单个镜像
test_mirror() {
local name=$1
local url=$2
local type=$3
echo -e "${BLUE}正在测试 ${name} (${type})...${NC}"
# 测试1: 基础连通性
local avg_time=0
local success_count=0
local total_speed=0
for i in $(seq 1 $TEST_COUNT); do
# 使用curl测试,获取总时间、连接时间、下载速度
result=$(curl -s -w "@curl-format.txt" -o /dev/null -m $TIMEOUT "${url}/${TEST_FILE}" 2>&1)
if [ $? -eq 0 ]; then
success_count=$((success_count + 1))
# 提取时间数据
time_total=$(echo "$result" | grep "time_total" | cut -d' ' -f2)
time_connect=$(echo "$result" | grep "time_connect" | cut -d' ' -f2)
speed_download=$(echo "$result" | grep "speed_download" | cut -d' ' -f2)
avg_time=$(echo "$avg_time + $time_total" | bc -l)
total_speed=$(echo "$total_speed + $speed_download" | bc -l)
echo -e " 测试 ${i}: ${GREEN}成功${NC} - 响应: ${time_connect}s, 总时间: ${time_total}s"
else
echo -e " 测试 ${i}: ${RED}失败${NC} - 超时或无法连接"
fi
# 两次测试间稍作间隔
sleep 0.5
done
# 计算平均值
if [ $success_count -gt 0 ]; then
avg_time=$(echo "scale=3; $avg_time / $success_count" | bc -l)
avg_speed=$(echo "scale=2; $total_speed / $success_count / 1024" | bc -l) # KB/s
echo -e " ${name}: ${GREEN}成功率 ${success_count}/${TEST_COUNT}${NC}, 平均响应: ${avg_time}s, 速度: ${avg_speed} KB/s"
# 返回结果
echo "${name}|${url}|${type}|${success_count}|${avg_time}|${avg_speed}"
else
echo -e " ${name}: ${RED}全部测试失败${NC}"
echo "${name}|${url}|${type}|0|999|0"
fi
echo ""
}
# 创建curl格式化输出文件
create_curl_format() {
cat > curl-format.txt << 'EOF'
time_namelookup: %{time_namelookup}
time_connect: %{time_connect}
time_appconnect: %{time_appconnect}
time_pretransfer: %{time_pretransfer}
time_redirect: %{time_redirect}
time_starttransfer: %{time_starttransfer}
time_total: %{time_total}
speed_download: %{speed_download}
http_code: %{http_code}
EOF
}
# 排序并显示结果
show_results() {
local results_file=$1
echo "=========================================="
echo " 测试结果汇总"
echo "=========================================="
echo ""
# 按成功率、平均时间、速度排序
echo -e "${GREEN}按成功率排序(推荐顺序):${NC}"
echo "-------------------------------------------"
# 读取结果并排序
while IFS='|' read -r name url type success avg_time avg_speed; do
if [ $success -gt 0 ]; then
printf "%-20s | 成功率: %d/%d | 平均时间: %.3fs | 速度: %.2f KB/s | 类型: %s\n" \
"$name" "$success" "$TEST_COUNT" "$avg_time" "$avg_speed" "$type"
else
printf "%-20s | ${RED}连接失败${NC}\n" "$name"
fi
done < <(sort -t'|' -k4,4nr -k5,5n -k6,6nr "$results_file")
echo ""
# 推荐最佳源
local best=$(head -n1 "$results_file" | sort -t'|' -k4,4nr -k5,5n -k6,6nr | head -n1)
IFS='|' read -r best_name best_url best_type best_success best_avg_time best_avg_speed <<< "$best"
if [ $best_success -gt 0 ]; then
echo -e "${GREEN}✨ 推荐源:${best_name} (${best_type})${NC}"
echo "URL: ${best_url}"
echo "平均响应时间: ${best_avg_time}秒"
echo "平均下载速度: ${best_avg_speed} KB/s"
echo ""
echo "配置示例(Debian 12):"
echo "-------------------------------------------"
echo -e "${YELLOW}# 编辑 sources.list 文件${NC}"
echo "sudo nano /etc/apt/sources.list"
echo ""
echo -e "${YELLOW}# 替换为以下内容${NC}"
echo "deb ${best_url} bookworm main contrib non-free non-free-firmware"
echo "deb ${best_url} bookworm-updates main contrib non-free non-free-firmware"
echo "deb ${best_url} bookworm-backports main contrib non-free non-free-firmware"
echo "deb http://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware"
echo ""
echo -e "${YELLOW}# 更新软件源${NC}"
echo "sudo apt update"
else
echo -e "${RED}所有镜像源都无法连接,请检查网络设置!${NC}"
fi
}
# 清理临时文件
cleanup() {
rm -f curl-format.txt
rm -f results.txt
}
# ==================== 主程序区 ====================
# 检查依赖
if ! command -v curl &> /dev/null; then
echo -e "${RED}错误:未安装 curl,请先安装 curl${NC}"
exit 1
fi
if ! command -v bc &> /dev/null; then
echo -e "${RED}错误:未安装 bc,请先安装 bc${NC}"
exit 1
fi
# 设置陷阱,确保脚本退出时清理临时文件
trap cleanup EXIT
# 创建临时文件
create_curl_format
touch results.txt
# 打印标题
print_header
# 测试每个镜像
for mirror in "${MIRRORS[@]}"; do
IFS='|' read -r name url type <<< "$mirror"
# 跳过IPv6专用源(如果本机不支持IPv6)
if [ "$IPV6_SUPPORT" = false ] && [[ $name == *"IPv6"* ]]; then
continue
fi
# 测试镜像
result=$(test_mirror "$name" "$url" "$type")
# 保存结果
if [[ $result == *"|"* ]]; then
echo "$result" >> results.txt
fi
done
# 显示结果
show_results results.txt
echo ""
echo "=========================================="
echo -e "${GREEN}测试完成!${NC}"
echo "=========================================="
# 清理会在trap中自动执行
使用说明
- 保存脚本:
# 将以上内容保存为 test_debian_mirrors.sh
nano test_debian_mirrors.sh
- 赋予执行权限:
chmod +x test_debian_mirrors.sh
- 运行测试:
# 基本用法
./test_debian_mirrors.sh
# 如果需要测试特定Debian版本,可以修改脚本中的 TEST_FILE 变量
# Debian 11: TEST_FILE="dists/bullseye/main/binary-amd64/Release"
# Debian 10: TEST_FILE="dists/buster/main/binary-amd64/Release"
高级配置
如果你想自定义测试参数,可以修改脚本开头的配置区:
TEST_COUNT:测试次数(默认为3次)TIMEOUT:超时时间(默认10秒)MIRRORS:添加或删除镜像源
依赖要求
脚本需要以下命令:
curl:用于测试HTTP连接bc:用于计算数值ip:用于检测IPv6支持(通常已预装)
安装依赖:
# Debian/Ubuntu
sudo apt update
sudo apt install curl bc -y
输出示例
==========================================
Debian 镜像源测速脚本
测试时间: 2025-11-09 12:30:45
==========================================
检测到你的网络支持IPv6
清华大学:
测试 1: 成功 - 响应: 0.023s, 总时间: 0.212s
测试 2: 成功 - 响应: 0.021s, 总时间: 0.205s
测试 3: 成功 - 响应: 0.025s, 总时间: 0.219s
清华大学: 成功率 3/3, 平均响应: 0.212s, 速度: 524.5 KB/s
然后根据测试结果,脚本会自动推荐最佳源并提供配置示例。
脚本的优点:
- 全面测试:响应时间、下载速度、成功率
- 智能排序:按成功率、响应时间、速度综合排序
- 自动推荐:给出最优源和配置命令
- IPv6支持:自动检测并使用IPv6
- 彩色输出:结果清晰易读
- 容错处理:超时和失败处理完善
你可以直接复制使用,也可以根据需要进行修改!
浙公网安备 33010602011771号