Update 测速中文

This commit is contained in:
2025-11-01 16:44:52 +08:00
committed by GitHub
parent e49e5ffa5b
commit c91e542d2e

View File

@@ -1,6 +1,6 @@
#!/bin/bash
# 精准网络测速一键脚本
# 精准网络测速一键脚本 - 终极修复版
# 支持多系统Ubuntu/Debian/CentOS/RHEL/Alpine
# 功能网络连通性测试、多节点延迟测试、Speedtest测速
@@ -59,7 +59,7 @@ install_tools() {
fi
# 安装必要工具
TOOLS=("curl" "wget" "ping" "bc" "pip3" "python3")
TOOLS=("curl" "wget" "ping" "bc" "python3")
for tool in "${TOOLS[@]}"; do
if ! check_command "$tool"; then
echo -e "${BLUE}安装 $tool...${NC}"
@@ -76,7 +76,7 @@ install_tools() {
"bc")
$INSTALL_CMD bc >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ bc 安装失败${NC}"
;;
"pip3"|"python3")
"python3")
if [ "$PM" = "apt-get" ]; then
$INSTALL_CMD python3 python3-pip >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ python3 安装失败${NC}"
elif [ "$PM" = "yum" ] || [ "$PM" = "dnf" ]; then
@@ -93,132 +93,171 @@ install_tools() {
done
}
# 清理和修复speedtest安装
clean_speedtest() {
echo -e "${BLUE}🧹 清理可能损坏的speedtest...${NC}"
# 终极speedtest安装方案
install_speedtest_ultimate() {
echo -e "${BLUE}🚀 终极speedtest安装方案...${NC}"
# 卸载各种版本的speedtest
apt-get remove -y speedtest-cli 2>/dev/null
pip uninstall -y speedtest-cli 2>/dev/null
pip3 uninstall -y speedtest-cli 2>/dev/null
rm -f /usr/local/bin/speedtest-cli
rm -f /usr/bin/speedtest
rm -f /usr/bin/speedtest-cli
# 方法1: 直接下载预编译的二进制文件(最可靠)
echo -e "${BLUE}方法1: 下载预编译二进制...${NC}"
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
BINARY_URL="https://github.com/tmplink/speedtest/raw/master/speedtest_x86_64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
BINARY_URL="https://github.com/tmplink/speedtest/raw/master/speedtest_aarch64"
else
echo -e "${YELLOW}⚠️ 不支持的架构: $ARCH尝试x86_64版本${NC}"
BINARY_URL="https://github.com/tmplink/speedtest/raw/master/speedtest_x86_64"
fi
echo -e "${GREEN}✅ 清理完成${NC}"
}
# 安装speedtest - 增强版本
install_speedtest() {
echo -e "${BLUE}📦 检查并安装speedtest...${NC}"
# 检查是否已安装
if check_command speedtest; then
echo -e "${GREEN}✅ speedtest 已安装${NC}"
speedtest --version
if curl -s -L -o /tmp/speedtest "$BINARY_URL" && chmod +x /tmp/speedtest; then
mv /tmp/speedtest /usr/local/bin/speedtest
echo -e "${GREEN}✅ 二进制speedtest安装成功${NC}"
return 0
fi
# 清理旧版本
clean_speedtest
# 方法2: 使用Python speedtest库
echo -e "${BLUE}方法2: 安装Python speedtest库...${NC}"
if check_command python3; then
cat > /usr/local/bin/speedtest.py << 'EOF'
#!/usr/bin/env python3
import speedtest
import sys
# 方法1: 安装官方ookla speedtest (首选)
echo -e "${BLUE}尝试安装官方speedtest...${NC}"
if check_command curl; then
if curl -s https://install.speedtest.net/app/cli/install.deb.sh | bash >/dev/null 2>&1; then
if $UPDATE_CMD >/dev/null 2>&1 && $INSTALL_CMD speedtest >/dev/null 2>&1; then
echo -e "${GREEN}✅ 官方speedtest安装成功${NC}"
speedtest --version
return 0
def main():
try:
s = speedtest.Speedtest()
s.get_best_server()
print("测试下载速度...")
download_speed = s.download() / 1024 / 1024 # 转换为Mbps
print(f"下载速度: {download_speed:.2f} Mbps")
print("测试上传速度...")
upload_speed = s.upload() / 1024 / 1024 # 转换为Mbps
print(f"上传速度: {upload_speed:.2f} Mbps")
print("测试延迟...")
ping = s.results.ping
print(f"网络延迟: {ping:.2f} ms")
except Exception as e:
print(f"测速失败: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
EOF
if python3 -c "import speedtest" 2>/dev/null; then
echo -e "${GREEN}✅ Python speedtest已安装${NC}"
else
if python3 -m pip install speedtest-cli >/dev/null 2>&1; then
echo -e "${GREEN}✅ Python speedtest安装成功${NC}"
fi
fi
chmod +x /usr/local/bin/speedtest.py
# 创建包装脚本
cat > /usr/local/bin/speedtest << 'EOF'
#!/bin/bash
/usr/bin/python3 /usr/local/bin/speedtest.py
EOF
chmod +x /usr/local/bin/speedtest
echo -e "${GREEN}✅ Python版speedtest安装成功${NC}"
return 0
fi
# 方法2: 直接下载二进制文件
echo -e "${BLUE}尝试下载二进制speedtest...${NC}"
if check_command wget; then
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
SPEEDTEST_URL="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-x86_64.tgz"
elif [ "$ARCH" = "aarch64" ]; then
SPEEDTEST_URL="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-aarch64.tgz"
else
SPEEDTEST_URL="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-x86_64.tgz"
fi
if wget -q -O speedtest.tgz "$SPEEDTEST_URL" && \
tar -xzf speedtest.tgz && \
mv speedtest /usr/local/bin/ && \
chmod +x /usr/local/bin/speedtest; then
echo -e "${GREEN}✅ 二进制speedtest安装成功${NC}"
speedtest --version
return 0
fi
rm -f speedtest.tgz
fi
# 方法3: 使用pip安装
echo -e "${BLUE}尝试pip安装speedtest-cli...${NC}"
if check_command pip3; then
if pip3 install speedtest-cli >/dev/null 2>&1; then
echo -e "${GREEN}✅ speedtest-cli pip安装成功${NC}"
return 0
fi
fi
# 方法4: 使用包管理器安装
echo -e "${BLUE}尝试包管理器安装...${NC}"
if [ "$PM" = "apt-get" ]; then
if $INSTALL_CMD speedtest-cli >/dev/null 2>&1; then
echo -e "${GREEN}✅ speedtest-cli 包管理器安装成功${NC}"
return 0
fi
elif [ "$PM" = "yum" ] || [ "$PM" = "dnf" ]; then
if $INSTALL_CMD speedtest-cli >/dev/null 2>&1; then
echo -e "${GREEN}✅ speedtest-cli 包管理器安装成功${NC}"
return 0
fi
fi
echo -e "${YELLOW}⚠️ speedtest安装失败将使用备用测速方法${NC}"
# 方法3: 使用wget和curl进行基本测速
echo -e "${YELLOW}⚠️ 无法安装标准speedtest将使用基础测速方法${NC}"
return 1
}
# 备用网络测速方法
backup_speed_test() {
echo -e "${CYAN}🔄 使用备用方法进行网络测速...${NC}"
# 增强版网络测速
enhanced_speed_test() {
echo -e "${CYAN}🌐 增强版网络测速...${NC}"
# 测试下载速度到多个测速服务器
servers=(
"http://speedtest.ftp.otenet.gr/files/test1Mb.db"
"http://ipv4.download.thinkbroadband.com/1MB.zip"
"http://proof.ovh.net/files/1Mb.dat"
# 测试国内CDN速度
echo -e "${BLUE}测试国内下载速度...${NC}"
domestic_servers=(
"https://mirrors.aliyun.com/ubuntu/ls-lR.gz"
"https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ls-lR.gz"
"https://mirrors.ustc.edu.cn/ubuntu/ls-lR.gz"
)
for server in "${servers[@]}"; do
echo -e "${BLUE}测试服务器: $(basename $server)${NC}"
if check_command wget; then
start_time=$(date +%s)
if wget -O /dev/null --timeout=10 --tries=2 "$server" >/dev/null 2>&1; then
end_time=$(date +%s)
duration=$((end_time - start_time))
if [ $duration -eq 0 ]; then
duration=1
for server in "${domestic_servers[@]}"; do
domain=$(echo "$server" | awk -F/ '{print $3}')
echo -e "测试 $domain ..."
if check_command curl; then
start_time=$(date +%s%N)
if curl -s --max-time 10 -o /dev/null "$server" 2>/dev/null; then
end_time=$(date +%s%N)
duration=$(( (end_time - start_time) / 1000000 )) # 毫秒
download_time=$(echo "scale=2; $duration / 1000" | bc -l 2>/dev/null || echo "0")
if [ "$download_time" != "0" ] && [ $(echo "$download_time > 0" | bc -l 2>/dev/null) -eq 1 ]; then
speed=$(echo "scale=2; 1.0 / $download_time * 8" | bc -l 2>/dev/null || echo "N/A")
echo -e "${GREEN} 下载速度: ${speed} Mbps${NC}"
break
else
echo -e "${YELLOW} 连接成功但速度计算失败${NC}"
fi
speed=$(echo "scale=2; 8 / $duration" | bc -l 2>/dev/null || echo "N/A")
echo -e "${GREEN} 下载速度: ${speed} Mbps${NC}"
break
else
echo -e "${YELLOW} 服务器不可用${NC}"
echo -e "${YELLOW} 连接失败${NC}"
fi
fi
done
# 使用iperf3测试如果可用
if check_command iperf3; then
echo -e "${BLUE}使用iperf3测试...${NC}"
iperf3 -c speedtest.serverius.net -p 5202 -t 5 -O 2 2>/dev/null | grep -E "sender|receiver" | head -2
# 测试国际速度
echo -e "${BLUE}测试国际连接速度...${NC}"
international_servers=(
"https://www.google.com/favicon.ico"
"https://www.github.com/favicon.ico"
"https://www.cloudflare.com/favicon.ico"
)
for server in "${international_servers[@]}"; do
domain=$(echo "$server" | awk -F/ '{print $3}')
echo -e "测试 $domain ..."
if check_command curl; then
if curl -s --max-time 10 -o /dev/null -w "HTTP状态: %{http_code}\n下载大小: %{size_download} bytes\n总时间: %{time_total} s\n" "$server"; then
echo -e "${GREEN} 连接成功${NC}"
break
else
echo -e "${YELLOW} 连接失败${NC}"
fi
fi
done
# 使用ping测试网络质量
echo -e "${BLUE}网络质量测试...${NC}"
test_hosts=("223.5.5.5" "114.114.114.114" "8.8.8.8" "1.1.1.1")
for host in "${test_hosts[@]}"; do
if ping -c 3 -W 2 "$host" >/dev/null 2>&1; then
result=$(ping -c 3 -W 2 "$host" 2>/dev/null | grep "packet loss" | awk '{print " 丢包率: "$6" | "}')
avg_ping=$(ping -c 3 -W 2 "$host" 2>/dev/null | grep "min/avg/max" | awk -F'/' '{print $5}')
echo -e "${GREEN}$host - 平均延迟: ${avg_ping}ms $result${NC}"
else
echo -e "${RED}$host - 无法连接${NC}"
fi
done
}
# 简单speedtest测速如果安装成功
simple_speedtest() {
echo -e "${CYAN}🚀 执行Speedtest测速...${NC}"
if check_command speedtest; then
echo -e "${GREEN}使用系统speedtest命令...${NC}"
if speedtest --simple 2>/dev/null; then
return 0
fi
fi
if [ -f /usr/local/bin/speedtest.py ] && check_command python3; then
echo -e "${GREEN}使用Python speedtest...${NC}"
python3 /usr/local/bin/speedtest.py
return 0
fi
echo -e "${YELLOW}⚠️ 未找到可用的speedtest使用增强测速...${NC}"
enhanced_speed_test
}
# 检查系统兼容性
@@ -240,6 +279,16 @@ check_system() {
# 检查内核版本
KERNEL=$(uname -r)
echo -e "${GREEN}✅ 内核版本: $KERNEL${NC}"
# 检查网络状态
echo -e "${BLUE}🌐 检查网络状态...${NC}"
if check_command curl; then
if curl -s --connect-timeout 5 https://www.baidu.com >/dev/null; then
echo -e "${GREEN}✅ 网络连接正常${NC}"
else
echo -e "${YELLOW}⚠️ 网络连接可能有限制${NC}"
fi
fi
}
# 网络连通性测试
@@ -259,7 +308,7 @@ test_connectivity() {
done
if [ "$connected" = false ]; then
handle_error "网络连接失败,请检查网络设置"
echo -e "${YELLOW}⚠️ 基础网络连接测试失败,但继续测试...${NC}"
fi
}
@@ -288,7 +337,6 @@ test_latency() {
echo -e "${RED}$result ❌${NC}"
fi
else
# 如果没有bc简单显示结果
echo -e "${GREEN}$result${NC}"
fi
else
@@ -300,53 +348,12 @@ test_latency() {
done
}
# Speedtest测速 - 增强版本
test_speed() {
echo -e "${CYAN}🚀 进行Speedtest测速...${NC}"
# 调试信息
echo -e "${BLUE}可用的speedtest命令:${NC}"
which speedtest 2>/dev/null && echo -e " speedtest: $(speedtest --version 2>/dev/null || echo '无法获取版本')"
which speedtest-cli 2>/dev/null && echo -e " speedtest-cli: $(speedtest-cli --version 2>/dev/null || echo '无法获取版本')"
# 检查是否有可用的speedtest命令
SPEEDTEST_CMD=""
if check_command speedtest; then
SPEEDTEST_CMD="speedtest --accept-license --accept-gdpr --simple"
elif check_command speedtest-cli; then
SPEEDTEST_CMD="speedtest-cli --simple"
elif [ -f /usr/local/bin/speedtest ]; then
SPEEDTEST_CMD="/usr/local/bin/speedtest --accept-license --accept-gdpr --simple"
fi
if [ -n "$SPEEDTEST_CMD" ]; then
echo -e "${BLUE}使用命令: $SPEEDTEST_CMD${NC}"
# 设置超时,防止卡住
timeout 60 bash -c "$SPEEDTEST_CMD" 2>/dev/null
result=$?
if [ $result -eq 0 ]; then
echo -e "${GREEN}✅ speedtest测速成功${NC}"
elif [ $result -eq 124 ]; then
echo -e "${YELLOW}⚠️ speedtest测速超时${NC}"
backup_speed_test
else
echo -e "${YELLOW}⚠️ speedtest测速失败错误码: $result${NC}"
backup_speed_test
fi
else
echo -e "${YELLOW}⚠️ 未找到speedtest命令${NC}"
backup_speed_test
fi
}
# 显示系统信息
show_system_info() {
echo -e "${PURPLE}"
echo "=================================================="
echo " 🌈 精准网络测速脚本 v3.0"
echo " 增强版 - 多方法测速 | 自动修复 | 备用方案"
echo " 🌈 终极网络测速脚本 v4.0"
echo " 专治各种网络测速失败问题"
echo " 支持: Ubuntu/Debian/CentOS/RHEL"
echo "=================================================="
echo -e "${NC}"
@@ -363,34 +370,16 @@ show_usage() {
echo -e "${GREEN}选项:${NC}"
echo -e " -h, --help 显示此帮助信息"
echo -e " -v, --version 显示版本信息"
echo -e " -f, --force 强制重新安装speedtest"
echo -e " -f, --fix 强制修复speedtest安装"
echo -e ""
echo -e "${GREEN}功能:${NC}"
echo -e " ✅ 系统环境检测"
echo -e " ✅ 自动安装依赖工具"
echo -e " ✅ 网络连通性测试"
echo -e " ✅ 多节点延迟测试"
echo -e " ✅ Speedtest网速测试 (多方法)"
echo -e " ✅ 备用测速方案"
echo -e " ✅ 自动修复安装问题"
echo -e " ✅ 终极speedtest安装"
echo -e " ✅ 增强版网络测速"
echo -e ""
echo -e "${GREEN}示例:${NC}"
echo -e " $0 # 运行完整测试"
echo -e " $0 --force # 强制重新安装speedtest"
echo -e " $0 --help # 显示帮助"
}
# 显示版本信息
show_version() {
echo -e "${GREEN}精准网络测速脚本 v3.0${NC}"
echo -e "增强版 - 多方法测速 | 自动修复 | 备用方案"
}
# 强制重新安装speedtest
force_reinstall_speedtest() {
echo -e "${YELLOW}🔄 强制重新安装speedtest...${NC}"
clean_speedtest
install_speedtest
}
# 主函数
@@ -402,38 +391,32 @@ main() {
exit 0
;;
-v|--version)
show_version
echo -e "${GREEN}终极网络测速脚本 v4.0${NC}"
exit 0
;;
-f|--force)
FORCE_REINSTALL=true
-f|--fix)
echo -e "${YELLOW}🛠️ 强制修复模式...${NC}"
;;
*)
# 正常执行
;;
esac
show_system_info
install_tools
echo ""
if [ "$FORCE_REINSTALL" = true ]; then
force_reinstall_speedtest
else
install_speedtest
fi
install_speedtest_ultimate
echo ""
test_connectivity
echo ""
test_latency
echo ""
test_speed
simple_speedtest
echo ""
echo -e "${PURPLE}==================================================${NC}"
echo -e "${PURPLE} 测试完成 🌟${NC}"
echo -e "${PURPLE}==================================================${NC}"
echo -e "${BLUE}💡 提示: 如果测速不理想,可尝试: $0 --fix${NC}"
}
# 设置陷阱,确保脚本退出时重置颜色