From 0726678ad10bbfa37c738f08bb8e326abb300a35 Mon Sep 17 00:00:00 2001 From: xzx3344521 Date: Sat, 1 Nov 2025 16:50:34 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=B5=8B=E9=80=9F=E4=B8=AD=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 测速中文 | 467 +++++++++++++++---------------------------------------- 1 file changed, 128 insertions(+), 339 deletions(-) diff --git a/测速中文 b/测速中文 index 8f7d680..4ae0bca 100644 --- a/测速中文 +++ b/测速中文 @@ -1,8 +1,7 @@ #!/bin/bash -# 精准网络测速一键脚本 - 终极修复版 +# 精准网络测速一键脚本 - 最终修复版 # 支持多系统:Ubuntu/Debian/CentOS/RHEL/Alpine -# 功能:网络连通性测试、多节点延迟测试、Speedtest测速 # 颜色定义 RED='\033[0;31m' @@ -13,12 +12,6 @@ PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' -# 错误处理函数 -handle_error() { - echo -e "${RED}❌ 错误: $1${NC}" - exit 1 -} - # 检查命令是否存在 check_command() { if ! command -v "$1" &> /dev/null; then @@ -27,399 +20,195 @@ check_command() { return 0 } -# 安装必要工具 +# 安装必要工具(简化版) install_tools() { - echo -e "${BLUE}📦 检查并安装必要工具...${NC}" + echo -e "${BLUE}📦 安装必要工具...${NC}" # 检测包管理器 if check_command apt-get; then PM="apt-get" - UPDATE_CMD="apt-get update" + 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" + UPDATE_CMD="yum update -y >/dev/null 2>&1" INSTALL_CMD="yum install -y" - elif check_command dnf; then - PM="dnf" - UPDATE_CMD="dnf update -y" - INSTALL_CMD="dnf install -y" - elif check_command apk; then - PM="apk" - UPDATE_CMD="apk update" - INSTALL_CMD="apk add" else - handle_error "未找到支持的包管理器" + echo -e "${YELLOW}⚠️ 使用系统自带工具${NC}" + return 0 fi # 更新包列表 - echo -e "${BLUE}更新包列表...${NC}" - if ! $UPDATE_CMD >/dev/null 2>&1; then - echo -e "${YELLOW}⚠️ 包列表更新失败,继续安装...${NC}" - fi + eval $UPDATE_CMD - # 安装必要工具 - TOOLS=("curl" "wget" "ping" "bc" "python3") - for tool in "${TOOLS[@]}"; do + # 只安装最必要的工具 + for tool in curl wget ping; do if ! check_command "$tool"; then echo -e "${BLUE}安装 $tool...${NC}" - case $tool in - "ping") - if [ "$PM" = "apt-get" ]; then - $INSTALL_CMD iputils-ping >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ iputils-ping 安装失败${NC}" - elif [ "$PM" = "yum" ] || [ "$PM" = "dnf" ]; then - $INSTALL_CMD iputils >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ iputils 安装失败${NC}" - elif [ "$PM" = "apk" ]; then - $INSTALL_CMD iputils >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ iputils 安装失败${NC}" - fi - ;; - "bc") - $INSTALL_CMD bc >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ bc 安装失败${NC}" - ;; - "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 - $INSTALL_CMD python3 python3-pip >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ python3 安装失败${NC}" - elif [ "$PM" = "apk" ]; then - $INSTALL_CMD python3 py3-pip >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ python3 安装失败${NC}" - fi - ;; - *) - $INSTALL_CMD "$tool" >/dev/null 2>&1 || echo -e "${YELLOW}⚠️ $tool 安装失败${NC}" - ;; - esac + $INSTALL_CMD "$tool" >/dev/null 2>&1 && echo -e "${GREEN}✅ $tool 安装成功${NC}" || echo -e "${YELLOW}⚠️ $tool 安装失败${NC}" fi done } -# 终极speedtest安装方案 -install_speedtest_ultimate() { - echo -e "${BLUE}🚀 终极speedtest安装方案...${NC}" +# 安装speedtest(终极方案) +install_speedtest_final() { + echo -e "${BLUE}🚀 安装Speedtest...${NC}" - # 方法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 + # 清理旧版本 + rm -f /usr/local/bin/speedtest /tmp/speedtest /usr/bin/speedtest - 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 - - # 方法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 - -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 - - # 方法3: 使用wget和curl进行基本测速 - echo -e "${YELLOW}⚠️ 无法安装标准speedtest,将使用基础测速方法${NC}" - return 1 -} - -# 增强版网络测速 -enhanced_speed_test() { - echo -e "${CYAN}🌐 增强版网络测速...${NC}" - - # 测试国内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 "${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 - else - echo -e "${YELLOW} 连接失败${NC}" - fi - fi - done - - # 测试国际速度 - 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 + # 方法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 - 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 + # 方法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 - - echo -e "${YELLOW}⚠️ 未找到可用的speedtest,使用增强测速...${NC}" - enhanced_speed_test + + # 方法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 } -# 检查系统兼容性 -check_system() { - echo -e "${BLUE}🔍 检查系统环境...${NC}" - - # 检查操作系统 - if [ -f /etc/os-release ]; then - . /etc/os-release - echo -e "${GREEN}✅ 操作系统: $PRETTY_NAME${NC}" +# 验证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 "${YELLOW}⚠️ 无法检测操作系统类型${NC}" + echo -e "${RED}❌ speedtest未安装${NC}" + return 1 fi +} - # 检查架构 - ARCH=$(uname -m) - echo -e "${GREEN}✅ 系统架构: $ARCH${NC}" - - # 检查内核版本 - KERNEL=$(uname -r) - echo -e "${GREEN}✅ 内核版本: $KERNEL${NC}" +# 终极网络测速方案 +ultimate_speed_test() { + echo -e "${CYAN}🚀 终极网络测速...${NC}" - # 检查网络状态 - echo -e "${BLUE}🌐 检查网络状态...${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 - if curl -s --connect-timeout 5 https://www.baidu.com >/dev/null; then - echo -e "${GREEN}✅ 网络连接正常${NC}" - else - echo -e "${YELLOW}⚠️ 网络连接可能有限制${NC}" - fi - fi -} - -# 网络连通性测试 -test_connectivity() { - echo -e "${BLUE}🌐 测试网络连接...${NC}" - - # 测试多个DNS服务器 - dns_servers=("223.5.5.5" "114.114.114.114" "8.8.8.8" "1.1.1.1") - connected=false - - for dns in "${dns_servers[@]}"; do - if ping -c 2 -W 3 "$dns" >/dev/null 2>&1; then - echo -e "${GREEN}✅ 网络连接正常 (通过 $dns)${NC}" - connected=true - break - fi - done - - if [ "$connected" = false ]; then - echo -e "${YELLOW}⚠️ 基础网络连接测试失败,但继续测试...${NC}" - fi -} - -# 多节点延迟测试 -test_latency() { - echo -e "${CYAN}🎯 多节点延迟测试${NC}" - nodes=("阿里DNS:223.5.5.5" "114DNS:114.114.114.114" "腾讯DNS:119.29.29.29" "Cloudflare:1.1.1.1" "Google DNS:8.8.8.8") - - for node in "${nodes[@]}"; do - name="${node%:*}" - ip="${node#*:}" - echo -e "${BLUE}测试 $name...${NC}" - - if ping -c 4 -W 2 "$ip" >/dev/null 2>&1; then - result=$(ping -c 4 -W 2 "$ip" 2>/dev/null | grep "min/avg/max" | awk -F'/' '{print " 平均延迟: "$5"ms"}') - if [ -n "$result" ]; then - latency=$(echo "$result" | grep -o '[0-9.]*') - if command -v bc >/dev/null 2>&1 && [ -n "$latency" ]; then - if (( $(echo "$latency < 50" | bc -l 2>/dev/null) )); then - echo -e "${GREEN}$result ✅${NC}" - elif (( $(echo "$latency < 100" | bc -l 2>/dev/null) )); then - echo -e "${GREEN}$result 👍${NC}" - elif (( $(echo "$latency < 200" | bc -l 2>/dev/null) )); then - echo -e "${YELLOW}$result ⚠️${NC}" - else - echo -e "${RED}$result ❌${NC}" - fi - else - echo -e "${GREEN}$result${NC}" - fi - else - echo -e "${YELLOW} 延迟测试结果解析失败${NC}" + # 测试到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} 测试失败${NC}" + echo -e "${RED}🌐 Cloudflare: 测试失败${NC}" fi - done + 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 " 🌈 终极网络测速脚本 v4.0" - echo " 专治各种网络测速失败问题" - echo " 支持: Ubuntu/Debian/CentOS/RHEL" + echo " 🌈 最终版网络测速脚本" + echo " 专为受限环境优化" echo "==================================================" echo -e "${NC}" - check_system + 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 "" } -# 显示使用说明 -show_usage() { - echo -e "${GREEN}使用方法:${NC}" - echo -e " $0 [选项]" - echo -e "" - echo -e "${GREEN}选项:${NC}" - echo -e " -h, --help 显示此帮助信息" - echo -e " -v, --version 显示版本信息" - 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 "" -} - # 主函数 main() { - # 处理命令行参数 - case "${1:-}" in - -h|--help) - show_usage - exit 0 - ;; - -v|--version) - echo -e "${GREEN}终极网络测速脚本 v4.0${NC}" - exit 0 - ;; - -f|--fix) - echo -e "${YELLOW}🛠️ 强制修复模式...${NC}" - ;; - *) - ;; - esac - show_system_info install_tools echo "" - install_speedtest_ultimate + install_speedtest_final echo "" - test_connectivity - echo "" - test_latency - echo "" - simple_speedtest + ultimate_speed_test echo "" echo -e "${PURPLE}==================================================${NC}" echo -e "${PURPLE} 测试完成 🌟${NC}" echo -e "${PURPLE}==================================================${NC}" - echo -e "${BLUE}💡 提示: 如果测速不理想,可尝试: $0 --fix${NC}" + echo -e "${BLUE}💡 提示: 绿色表示正常,红色表示异常${NC}" } -# 设置陷阱,确保脚本退出时重置颜色 +# 设置陷阱 trap 'echo -e "${NC}"' EXIT # 运行主函数