Files
dock/测速中文
2025-11-01 16:44:52 +08:00

427 lines
14 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 精准网络测速一键脚本 - 终极修复版
# 支持多系统Ubuntu/Debian/CentOS/RHEL/Alpine
# 功能网络连通性测试、多节点延迟测试、Speedtest测速
# 颜色定义
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'
# 错误处理函数
handle_error() {
echo -e "${RED}❌ 错误: $1${NC}"
exit 1
}
# 检查命令是否存在
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"
INSTALL_CMD="apt-get install -y"
elif check_command yum; then
PM="yum"
UPDATE_CMD="yum update -y"
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 "未找到支持的包管理器"
fi
# 更新包列表
echo -e "${BLUE}更新包列表...${NC}"
if ! $UPDATE_CMD >/dev/null 2>&1; then
echo -e "${YELLOW}⚠️ 包列表更新失败,继续安装...${NC}"
fi
# 安装必要工具
TOOLS=("curl" "wget" "ping" "bc" "python3")
for tool in "${TOOLS[@]}"; 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
fi
done
}
# 终极speedtest安装方案
install_speedtest_ultimate() {
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
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
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
}
# 检查系统兼容性
check_system() {
echo -e "${BLUE}🔍 检查系统环境...${NC}"
# 检查操作系统
if [ -f /etc/os-release ]; then
. /etc/os-release
echo -e "${GREEN}✅ 操作系统: $PRETTY_NAME${NC}"
else
echo -e "${YELLOW}⚠️ 无法检测操作系统类型${NC}"
fi
# 检查架构
ARCH=$(uname -m)
echo -e "${GREEN}✅ 系统架构: $ARCH${NC}"
# 检查内核版本
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
}
# 网络连通性测试
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}"
fi
else
echo -e "${RED} 测试失败${NC}"
fi
done
}
# 显示系统信息
show_system_info() {
echo -e "${PURPLE}"
echo "=================================================="
echo " 🌈 终极网络测速脚本 v4.0"
echo " 专治各种网络测速失败问题"
echo " 支持: Ubuntu/Debian/CentOS/RHEL"
echo "=================================================="
echo -e "${NC}"
check_system
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
echo ""
test_connectivity
echo ""
test_latency
echo ""
simple_speedtest
echo ""
echo -e "${PURPLE}==================================================${NC}"
echo -e "${PURPLE} 测试完成 🌟${NC}"
echo -e "${PURPLE}==================================================${NC}"
echo -e "${BLUE}💡 提示: 如果测速不理想,可尝试: $0 --fix${NC}"
}
# 设置陷阱,确保脚本退出时重置颜色
trap 'echo -e "${NC}"' EXIT
# 运行主函数
main "$@"