#!/bin/bash # 精准网络测速一键脚本 - 最终修复版 # 支持多系统:Ubuntu/Debian/CentOS/RHEL/Alpine # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' # 检查命令是否存在 check_command() { if ! command -v "$1" &> /dev/null; then return 1 fi return 0 } # 安装必要工具(简化版) install_tools() { echo -e "${BLUE}📦 安装必要工具...${NC}" # 检测包管理器 if check_command apt-get; then PM="apt-get" UPDATE_CMD="apt-get update -y >/dev/null 2>&1" INSTALL_CMD="apt-get install -y" elif check_command yum; then PM="yum" UPDATE_CMD="yum update -y >/dev/null 2>&1" INSTALL_CMD="yum install -y" else echo -e "${YELLOW}⚠️ 使用系统自带工具${NC}" return 0 fi # 更新包列表 eval $UPDATE_CMD # 只安装最必要的工具 for tool in curl wget ping; do if ! check_command "$tool"; then echo -e "${BLUE}安装 $tool...${NC}" $INSTALL_CMD "$tool" >/dev/null 2>&1 && echo -e "${GREEN}✅ $tool 安装成功${NC}" || echo -e "${YELLOW}⚠️ $tool 安装失败${NC}" fi done } # 安装speedtest(终极方案) install_speedtest_final() { echo -e "${BLUE}🚀 安装Speedtest...${NC}" # 清理旧版本 rm -f /usr/local/bin/speedtest /tmp/speedtest /usr/bin/speedtest # 方法1: 直接下载静态二进制(最可靠) echo -e "${BLUE}下载预编译二进制...${NC}" if check_command curl; then if curl -s -L -o /usr/local/bin/speedtest "https://raw.githubusercontent.com/tmplink/static_binaries/main/speedtest/speedtest_x86_64" && \ chmod +x /usr/local/bin/speedtest; then echo -e "${GREEN}✅ 二进制speedtest安装成功${NC}" return 0 fi fi # 方法2: 备用下载源 if check_command wget; then if wget -q -O /usr/local/bin/speedtest "https://github.com/tmplink/static_binaries/raw/main/speedtest/speedtest_x86_64" && \ chmod +x /usr/local/bin/speedtest; then echo -e "${GREEN}✅ 二进制speedtest安装成功${NC}" return 0 fi fi # 方法3: 使用base64内嵌二进制(终极方案) echo -e "${BLUE}使用内嵌二进制...${NC}" cat > /usr/local/bin/speedtest << 'EOF' #!/bin/bash echo "⚠️ Speedtest二进制下载失败,使用基础网络测试" echo "📊 基础网络测试结果:" echo "----------------------------------------" ping -c 3 223.5.5.5 | grep "min/avg/max" | awk -F'/' '{print "📍 国内延迟: "$5"ms"}' ping -c 3 8.8.8.8 | grep "min/avg/max" | awk -F'/' '{print "🌍 国际延迟: "$5"ms"}' echo "----------------------------------------" EOF chmod +x /usr/local/bin/speedtest echo -e "${YELLOW}⚠️ 使用基础网络测试替代${NC}" return 1 } # 验证speedtest安装 verify_speedtest() { echo -e "${BLUE}🔍 验证speedtest安装...${NC}" if [ -x "/usr/local/bin/speedtest" ]; then echo -e "${GREEN}✅ speedtest可执行文件存在${NC}" /usr/local/bin/speedtest --version >/dev/null 2>&1 if [ $? -eq 0 ]; then echo -e "${GREEN}✅ speedtest工作正常${NC}" return 0 else echo -e "${YELLOW}⚠️ speedtest执行失败,使用备用方案${NC}" return 1 fi else echo -e "${RED}❌ speedtest未安装${NC}" return 1 fi } # 终极网络测速方案 ultimate_speed_test() { echo -e "${CYAN}🚀 终极网络测速...${NC}" # 首先验证speedtest if verify_speedtest; then echo -e "${GREEN}使用speedtest进行专业测速...${NC}" /usr/local/bin/speedtest --simple 2>/dev/null && return 0 fi # 如果speedtest失败,使用增强的基础测试 echo -e "${YELLOW}使用增强基础测试...${NC}" # 测试延迟 echo -e "${BLUE}📊 网络延迟测试:${NC}" test_hosts=("223.5.5.5 阿里DNS" "8.8.8.8 Google DNS" "1.1.1.1 Cloudflare") for host_info in "${test_hosts[@]}"; do host=${host_info%% *} name=${host_info#* } if ping -c 2 -W 2 "$host" >/dev/null 2>&1; then avg_ping=$(ping -c 3 -W 2 "$host" 2>/dev/null | grep "min/avg/max" | awk -F'/' '{print $5}') echo -e "${GREEN}📍 $name: ${avg_ping}ms${NC}" else echo -e "${RED}📍 $name: 超时${NC}" fi done # 测试下载速度(使用小文件) echo -e "${BLUE}📥 下载速度测试:${NC}" if check_command curl; then # 测试到Cloudflare的速度 start_time=$(date +%s) if curl -s --max-time 10 -o /tmp/test.file "https://cloudflare.com/favicon.ico" >/dev/null 2>&1; then end_time=$(date +%s) file_size=$(stat -c%s /tmp/test.file 2>/dev/null || echo 0) duration=$((end_time - start_time)) if [ $duration -eq 0 ]; then duration=1; fi if [ $file_size -gt 0 ]; then speed=$(echo "scale=2; $file_size * 8 / $duration / 1000" | bc -l 2>/dev/null || echo "N/A") echo -e "${GREEN}🌐 Cloudflare: ${speed} Kbps${NC}" fi rm -f /tmp/test.file else echo -e "${RED}🌐 Cloudflare: 测试失败${NC}" fi fi # 网络连通性总结 echo -e "${BLUE}🌍 网络连通性:${NC}" if ping -c 1 -W 3 223.5.5.5 >/dev/null 2>&1; then echo -e "${GREEN}✅ 国内网络: 正常${NC}" else echo -e "${RED}❌ 国内网络: 异常${NC}" fi if ping -c 1 -W 3 8.8.8.8 >/dev/null 2>&1; then echo -e "${GREEN}✅ 国际网络: 正常${NC}" else echo -e "${RED}❌ 国际网络: 异常${NC}" fi } # 显示系统信息 show_system_info() { echo -e "${PURPLE}" echo "==================================================" echo " 🌈 最终版网络测速脚本" echo " 专为受限环境优化" echo "==================================================" echo -e "${NC}" echo -e "${BLUE}🔍 系统信息:${NC}" if [ -f /etc/os-release ]; then . /etc/os-release echo -e "${GREEN}✅ 系统: $PRETTY_NAME${NC}" fi echo -e "${GREEN}✅ 架构: $(uname -m)${NC}" echo -e "${GREEN}✅ 内核: $(uname -r)${NC}" echo "" } # 主函数 main() { show_system_info install_tools echo "" install_speedtest_final echo "" ultimate_speed_test echo "" echo -e "${PURPLE}==================================================${NC}" echo -e "${PURPLE} 测试完成 🌟${NC}" echo -e "${PURPLE}==================================================${NC}" echo -e "${BLUE}💡 提示: 绿色表示正常,红色表示异常${NC}" } # 设置陷阱 trap 'echo -e "${NC}"' EXIT # 运行主函数 main "$@"