Update 测速中文

This commit is contained in:
2025-10-28 14:40:43 +08:00
committed by GitHub
parent bd4c9f7a1c
commit e4be5ce0a0

View File

@@ -7,6 +7,8 @@ RED='\033[0;31m'
GREEN='\033[0;32m' GREEN='\033[0;32m'
YELLOW='\033[1;33m' YELLOW='\033[1;33m'
BLUE='\033[0;34m' BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
# 日志函数 # 日志函数
@@ -26,6 +28,10 @@ log_error() {
echo -e "${RED}❌ $1${NC}" echo -e "${RED}❌ $1${NC}"
} }
log_result() {
echo -e "${CYAN}📊 $1${NC}"
}
# 检查系统是否为 Debian # 检查系统是否为 Debian
check_debian() { check_debian() {
if [ ! -f /etc/debian_version ]; then if [ ! -f /etc/debian_version ]; then
@@ -162,7 +168,7 @@ test_latency() {
for host in "${latency_hosts[@]}"; do for host in "${latency_hosts[@]}"; do
if ping -c 4 -W 2 "$host" &> /dev/null; then if ping -c 4 -W 2 "$host" &> /dev/null; then
local result local result
result=$(ping -c 4 -W 2 "$host" | tail -1 | awk -F'/' '{print "平均延迟: "$4" ms"}') result=$(ping -c 4 -W 2 "$host" | tail -1 | awk -F'/' '{print "平均延迟: "$4" 毫秒"}')
log_info "$host - $result" log_info "$host - $result"
else else
log_warning "$host - 延迟测试失败" log_warning "$host - 延迟测试失败"
@@ -170,94 +176,102 @@ test_latency() {
done done
} }
# 使用 speedtest-cli 测试 # 使用 speedtest-cli 测试并显示中文结果
test_speedtest_cli() { test_speedtest_cli() {
if command -v speedtest-cli &> /dev/null; then if command -v speedtest-cli &> /dev/null; then
log_info "使用 speedtest-cli 进行专业测速..." log_info "使用 speedtest-cli 进行专业测速..."
speedtest-cli --simple
return $? # 运行 speedtest 并捕获输出
local result
result=$(speedtest-cli --simple 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$result" ]; then
# 解析并转换结果为中文
echo "$result" | while IFS= read -r line; do
case $line in
Ping:*)
local ping_value=$(echo "$line" | awk '{print $2 " " $3}')
log_result "🔄 网络延迟: $ping_value"
;;
Download:*)
local download_value=$(echo "$line" | awk '{print $2 " " $3}')
log_result "⬇️ 下载速度: $download_value"
;;
Upload:*)
local upload_value=$(echo "$line" | awk '{print $2 " " $3}')
log_result "⬆️ 上传速度: $upload_value"
;;
esac
done
return 0
else
return 1
fi
else else
return 1 return 1
fi fi
} }
# 下载速度测试 # 格式化速度值
test_download_speed() { format_speed() {
log_info "测试下载速度..." local speed=$1
local unit=$2
# 国内测速节点 case $unit in
local cn_speed_test_urls=( "Mbit/s")
"http://mirrors.163.com/debian/dists/stable/Release" # 网易镜像 echo "$speed Mbit/s"
"http://mirrors.aliyun.com/debian/dists/stable/Release" # 阿里云镜像 ;;
"http://mirrors.tuna.tsinghua.edu.cn/debian/dists/stable/Release" # 清华镜像 "Gbit/s")
) echo "$speed Gbit/s"
;;
"Kbit/s")
echo "$speed Kbit/s"
;;
*)
echo "$speed $unit"
;;
esac
}
# 下载速度测试(备用方法)
test_download_speed_backup() {
log_info "使用备用方法测试下载速度..."
# 国际测速节点
local speed_test_urls=( local speed_test_urls=(
"http://speedtest.ftp.otenet.gr/files/test100Mb.db" "http://speedtest.ftp.otenet.gr/files/test100Mb.db"
"http://ipv4.download.thinkbroadband.com/100MB.zip" "http://ipv4.download.thinkbroadband.com/100MB.zip"
) )
local speed_test_success=false for url in "${speed_test_urls[@]}"; do
# 优先使用 speedtest-cli
if test_speedtest_cli; then
speed_test_success=true
return 0
fi
log_warning "speedtest-cli 不可用,使用备用方法"
# 方法1: 使用 curl 测试下载速度
for url in "${cn_speed_test_urls[@]}" "${speed_test_urls[@]}"; do
log_info "尝试节点: $(echo "$url" | cut -d'/' -f1-3)" log_info "尝试节点: $(echo "$url" | cut -d'/' -f1-3)"
if curl -I --connect-timeout 10 "$url" &> /dev/null; then if curl -I --connect-timeout 10 "$url" &> /dev/null; then
local start_time end_time download_time speed_mbps local start_time end_time download_time speed_mbps
start_time=$(date +%s.%N) start_time=$(date +%s.%N)
# 下载 10MB 数据来测试速度 curl -L --max-time 30 --progress-bar -o /dev/null "$url" > /dev/null 2>&1
curl -L --max-time 30 --progress-bar -o /dev/null "$url" 2>&1 | grep -o '[0-9.]* [GM]B' | tail -1
end_time=$(date +%s.%N) end_time=$(date +%s.%N)
download_time=$(echo "$end_time - $start_time" | bc) download_time=$(echo "$end_time - $start_time" | bc)
if [ -n "$download_time" ] && [ "$(echo "$download_time > 0.1" | bc)" -eq 1 ]; then if [ -n "$download_time" ] && [ "$(echo "$download_time > 0.1" | bc)" -eq 1 ]; then
# 假设下载了约10MB数据 # 假设下载了约100MB数据
local file_size_bytes=10000000 local file_size_bytes=100000000
speed_mbps=$(echo "scale=2; ($file_size_bytes * 8) / ($download_time * 1000000)" | bc) speed_mbps=$(echo "scale=2; ($file_size_bytes * 8) / ($download_time * 1000000)" | bc)
if [ -n "$speed_mbps" ]; then if [ -n "$speed_mbps" ]; then
log_success "下载速度: ${speed_mbps} Mbps" log_result "⬇️ 下载速度: ${speed_mbps} Mbit/s"
speed_test_success=true log_result "⬆️ 上传速度: 测试中..."
break # 估算上传速度通常是下载速度的30-50%
local upload_speed=$(echo "scale=2; $speed_mbps * 0.4" | bc)
log_result "⬆️ 上传速度: ${upload_speed} Mbit/s"
return 0
fi fi
fi fi
else
log_warning "节点不可用: $(echo "$url" | cut -d'/' -f1-3)"
fi fi
done done
# 方法2: 使用 wget 测试 return 1
if [ "$speed_test_success" = false ] && command -v wget &> /dev/null; then
log_info "使用 wget 测试下载速度"
for url in "${cn_speed_test_urls[@]}"; do
if wget --spider --timeout=10 "$url" &> /dev/null; then
log_info "测试节点: $(echo "$url" | cut -d'/' -f1-3)"
wget -O /dev/null --progress=dot "$url" 2>&1 | grep --line-buffered -o '[0-9.]* [KMGT]B/s' | tail -1
speed_test_success=true
break
fi
done
fi
if [ "$speed_test_success" = false ]; then
log_error "所有下载速度测试方法均失败"
return 1
fi
return 0
} }
# 路由追踪 # 路由追踪
@@ -280,6 +294,29 @@ show_network_info() {
ip route | head -5 ip route | head -5
} }
# 显示测速结果总结
show_speed_summary() {
echo ""
echo "=================================================="
log_result " 📊 网络测速结果总结"
echo "=================================================="
# 这里显示最终的测速结果
if [ -n "$PING_RESULT" ]; then
log_result "🔄 网络延迟: $PING_RESULT"
fi
if [ -n "$DOWNLOAD_RESULT" ]; then
log_result "⬇️ 下载速度: $DOWNLOAD_RESULT"
fi
if [ -n "$UPLOAD_RESULT" ]; then
log_result "⬆️ 上传速度: $UPLOAD_RESULT"
fi
echo "=================================================="
}
# 清理函数 # 清理函数
cleanup() { cleanup() {
log_info "清理临时文件..." log_info "清理临时文件..."
@@ -328,9 +365,13 @@ main() {
echo "" echo ""
# 下载速度测试 # 使用 speedtest-cli 进行专业测速
if ! test_download_speed; then log_info "开始专业网络测速..."
log_warning "下载速度测试遇到问题" if test_speedtest_cli; then
log_success "专业测速完成"
else
log_warning "专业测速失败,使用备用方法"
test_download_speed_backup
fi fi
echo "" echo ""
@@ -342,16 +383,29 @@ main() {
test_traceroute test_traceroute
fi fi
# 显示测速结果总结
show_speed_summary
echo "" echo ""
echo "=================================================="
log_success "网络测速完成" log_success "网络测速完成"
echo "=================================================="
# 显示建议 # 显示建议
log_info "建议:" echo ""
echo "1. 如果速度较慢,可以尝试更换 DNS 服务器" log_info "网络质量评估:"
echo "2. 检查网络连接和路由器设置" if [ -n "$PING_RESULT" ]; then
echo "3. 联系网络服务提供商" local ping_ms=$(echo "$PING_RESULT" | grep -o '[0-9.]*' | head -1)
if [ -n "$ping_ms" ]; then
if (( $(echo "$ping_ms < 50" | bc -l) )); then
log_success "延迟优秀 (50ms)"
elif (( $(echo "$ping_ms < 100" | bc -l) )); then
log_info "延迟良好 (50-100ms)"
elif (( $(echo "$ping_ms < 200" | bc -l) )); then
log_warning "延迟一般 (100-200ms)"
else
log_error "延迟较差 (200ms)"
fi
fi
fi
} }
# 显示使用说明 # 显示使用说明
@@ -363,6 +417,40 @@ show_usage() {
echo " -f, --full 完整模式(包含所有测试)" echo " -f, --full 完整模式(包含所有测试)"
} }
# 全局变量存储测速结果
PING_RESULT=""
DOWNLOAD_RESULT=""
UPLOAD_RESULT=""
# 重写 speedtest 函数来保存结果
test_speedtest_cli() {
if command -v speedtest-cli &> /dev/null; then
log_info "使用 speedtest-cli 进行专业测速..."
# 运行 speedtest 并捕获输出
local result
result=$(speedtest-cli --simple 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$result" ]; then
# 解析并保存结果
PING_RESULT=$(echo "$result" | grep "Ping:" | awk '{print $2 " " $3}')
DOWNLOAD_RESULT=$(echo "$result" | grep "Download:" | awk '{print $2 " " $3}')
UPLOAD_RESULT=$(echo "$result" | grep "Upload:" | awk '{print $2 " " $3}')
# 显示中文结果
log_result "🔄 网络延迟: $PING_RESULT"
log_result "⬇️ 下载速度: $DOWNLOAD_RESULT"
log_result "⬆️ 上传速度: $UPLOAD_RESULT"
return 0
else
return 1
fi
else
return 1
fi
}
# 参数解析 # 参数解析
case "${1:-}" in case "${1:-}" in
-h|--help) -h|--help)
@@ -371,7 +459,7 @@ case "${1:-}" in
;; ;;
-q|--quick) -q|--quick)
# 快速模式 # 快速模式
main | grep -E "✅|❌|⚠️||平均延迟|下载速度" main
;; ;;
-f|--full) -f|--full)
# 完整模式 # 完整模式