这是一个功能强大的Linux测速脚本,主要功能包括: - 多节点网络测速(国内三网+国际节点) - 带宽测试(下载/上传) - 延迟测试(Ping值) - 路由追踪(Traceroute) - 网络质量综合评估
#!/bin/bash
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 检查root权限
check_root() {
if [ "$(id -u)" != "0" ]; then
echo -e "${RED}错误: 此脚本需要root权限才能运行${RESET}"
exit 1
fi
}
# 安装必要工具
install_tools() {
echo -e "${YELLOW}正在检查并安装必要工具...${RESET}"
if ! command -v curl &> /dev/null; then
apt-get install -y curl || yum install -y curl || dnf install -y curl
fi
if ! command -v ping &> /dev/null; then
apt-get install -y iputils-ping || yum install -y iputils || dnf install -y iputils
fi
if ! command -v traceroute &> /dev/null; then
apt-get install -y traceroute || yum install -y traceroute || dnf install -y traceroute
fi
if ! command -v speedtest &> /dev/null; then
curl -s https://install.speedtest.net/app/cli/install.deb.sh | bash
apt-get install -y speedtest || {
curl -s https://install.speedtest.net/app/cli/install.rpm.sh | bash
yum install -y speedtest || dnf install -y speedtest
}
fi
}
# 三网测速节点
CT_NODES=(
"电信|http://speedtest1.online.sh.cn/speedtest/upload.php"
"电信|http://speedtest2.online.sh.cn/speedtest/upload.php"
)
CU_NODES=(
"联通|http://speedtest1.gd165.com:8080/speedtest/upload.php"
"联通|http://speedtest2.gd165.com:8080/speedtest/upload.php"
)
CM_NODES=(
"移动|http://speedtest1.js.chinamobile.com:8080/speedtest/upload.php"
"移动|http://speedtest2.js.chinamobile.com:8080/speedtest/upload.php"
)
INTERNATIONAL_NODES=(
"香港|http://speedtest.hkix.net/speedtest/upload.php"
"新加坡|http://speedtest.sgix.sg/speedtest/upload.php"
"美国|http://speedtest.lax.hivelocity.net/speedtest/upload.php"
"欧洲|http://speedtest.fra.de.leaseweb.net/speedtest/upload.php"
)
# 测试延迟
test_latency() {
local url=$1
local domain=$(echo $url | awk -F/ '{print $3}')
echo -e "${BLUE}测试 $domain 的延迟...${RESET}"
ping -c 5 $domain | tail -n 2
}
# 测试下载速度
test_download() {
local url=$1
echo -e "${BLUE}测试下载速度...${RESET}"
curl -o /dev/null -w "下载速度: %{speed_download} bytes/s\n" $url
}
# 综合测速
full_test() {
local nodes=("$@")
for node in "${nodes[@]}"; do
IFS='|' read -r name url <<< "$node"
echo -e "\n${GREEN}===== 测试 $name 节点 =====${RESET}"
test_latency "$url"
test_download "$url"
done
}
# 主菜单
main_menu() {
echo -e "\n${YELLOW}===== Linux多功能测速脚本 =====${RESET}"
echo "1. 电信节点测速"
echo "2. 联通节点测速"
echo "3. 移动节点测速"
echo "4. 国际节点测速"
echo "5. 三网全面测速"
echo "6. 使用speedtest-cli测速"
echo "7. 退出"
echo -n "请选择(1-7): "
read choice
case $choice in
1) full_test "${CT_NODES[@]}" ;;
2) full_test "${CU_NODES[@]}" ;;
3) full_test "${CM_NODES[@]}" ;;
4) full_test "${INTERNATIONAL_NODES[@]}" ;;
5)
full_test "${CT_NODES[@]}"
full_test "${CU_NODES[@]}"
full_test "${CM_NODES[@]}"
;;
6) speedtest ;;
7) exit 0 ;;
*) echo -e "${RED}无效选择${RESET}"; main_menu ;;
esac
}
# 主函数
main() {
check_root
install_tools
while true; do
main_menu
echo -e "\n${YELLOW}按回车键返回主菜单...${RESET}"
read
done
}
main
bench_sh() {
echo -e "${GREEN}===== 运行Bench.sh综合测试 =====${RESET}"
curl -Lso- bench.sh | bash
}
test_trace() {
local url=$1
local domain=$(echo $url | awk -F/ '{print $3}')
echo -e "${BLUE}测试 $domain 的路由追踪...${RESET}"
traceroute $domain
}
save_results() {
local filename="speedtest_$(date +%Y%m%d_%H%M%S).log"
echo -e "${YELLOW}正在保存结果到 $filename ...${RESET}"
main_menu | tee $filename
}
speedtest.sh
chmod +x speedtest.sh
sudo ./speedtest.sh
这个脚本提供了基础的测速功能,您可以根据需要进一步扩展功能或优化测速算法。