AC2100/route_test.sh

213 lines
8.2 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
# ============================================================
# 三网路由检测脚本 (电信 / 联通 / 移动)
# 适用系统: Debian / Ubuntu
# 功能: 自动安装依赖,测试去程+回程路由
# ============================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
RESET='\033[0m'
# ── 测试目标 IP ──────────────────────────────────────────────
# 去程从本地到这些国内IP
TELECOM_IPS=("202.96.209.5" "61.139.2.69" "211.137.58.20") # 电信各省
UNICOM_IPS=("211.95.46.3" "218.104.111.114" "61.135.169.121") # 联通各省
MOBILE_IPS=("221.179.131.11" "223.5.5.5" "120.196.165.1") # 移动各省
# 回程测试目标
TELECOM_RETURN="180.97.33.108" # 电信上海
UNICOM_RETURN="218.104.111.114" # 联通武汉
MOBILE_RETURN="221.179.131.11" # 移动北京
# ── 辅助函数 ─────────────────────────────────────────────────
print_banner() {
echo -e "${CYAN}"
echo "╔══════════════════════════════════════════════════╗"
echo "║ 三网路由检测脚本 by route_test ║"
echo "║ 电信 / 联通 / 移动 去程 + 回程 ║"
echo "╚══════════════════════════════════════════════════╝"
echo -e "${RESET}"
}
print_section() {
echo ""
echo -e "${BOLD}${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
echo -e "${BOLD}${YELLOW} $1${RESET}"
echo -e "${BOLD}${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
}
check_root() {
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}[错误] 请使用 root 用户运行此脚本${RESET}"
echo " sudo bash route_test.sh"
exit 1
fi
}
get_vps_info() {
print_section "📋 VPS 基本信息"
local ip=$(curl -s --max-time 5 ip.sb 2>/dev/null || echo "获取失败")
local info=$(curl -s --max-time 5 ipinfo.io 2>/dev/null)
local org=$(echo "$info" | grep '"org"' | cut -d'"' -f4)
local city=$(echo "$info" | grep '"city"' | cut -d'"' -f4)
local country=$(echo "$info" | grep '"country"' | cut -d'"' -f4)
echo -e " IP地址 : ${GREEN}${ip}${RESET}"
echo -e " 归属地 : ${GREEN}${country} ${city}${RESET}"
echo -e " ASN/运营: ${GREEN}${org}${RESET}"
echo -e " 系统 : ${GREEN}$(lsb_release -d 2>/dev/null | cut -f2)${RESET}"
echo -e " 内核 : ${GREEN}$(uname -r)${RESET}"
echo -e " 时间 : ${GREEN}$(date '+%Y-%m-%d %H:%M:%S %Z')${RESET}"
}
install_deps() {
print_section "🔧 检查并安装依赖"
apt-get update -qq 2>/dev/null
# mtr
if ! command -v mtr &>/dev/null; then
echo -e " ${YELLOW}安装 mtr...${RESET}"
apt-get install -y -qq mtr-tiny 2>/dev/null && \
echo -e " ${GREEN}✔ mtr 安装完成${RESET}" || \
echo -e " ${RED}✘ mtr 安装失败${RESET}"
else
echo -e " ${GREEN}✔ mtr 已安装${RESET}"
fi
# traceroute
if ! command -v traceroute &>/dev/null; then
echo -e " ${YELLOW}安装 traceroute...${RESET}"
apt-get install -y -qq traceroute 2>/dev/null && \
echo -e " ${GREEN}✔ traceroute 安装完成${RESET}" || \
echo -e " ${RED}✘ traceroute 安装失败${RESET}"
else
echo -e " ${GREEN}✔ traceroute 已安装${RESET}"
fi
# nexttrace
if ! command -v nexttrace &>/dev/null; then
echo -e " ${YELLOW}安装 nexttrace...${RESET}"
curl -s https://raw.githubusercontent.com/nxtrace/NTrace-core/main/nt_install.sh | bash -s -- --yes 2>/dev/null
if command -v nexttrace &>/dev/null; then
echo -e " ${GREEN}✔ nexttrace 安装完成${RESET}"
else
echo -e " ${RED}✘ nexttrace 安装失败,回程测试将跳过${RESET}"
fi
else
echo -e " ${GREEN}✔ nexttrace 已安装$(nexttrace --version 2>/dev/null | head -1)${RESET}"
fi
}
# ── 去程测试ping延迟────────────────────────────────────────
ping_test() {
local label=$1
local ip=$2
local result=$(ping -c 3 -W 3 "$ip" 2>/dev/null | tail -1 | awk -F'/' '{print $5}')
if [[ -n "$result" ]]; then
echo -e " ${ip}${GREEN}${result} ms${RESET}"
else
echo -e " ${ip}${RED}超时/不可达${RESET}"
fi
}
outbound_test() {
print_section "📤 去程延迟测试VPS → 国内)"
echo -e "\n ${BOLD}▶ 电信${RESET}"
for ip in "${TELECOM_IPS[@]}"; do
ping_test "电信" "$ip"
done
echo -e "\n ${BOLD}▶ 联通${RESET}"
for ip in "${UNICOM_IPS[@]}"; do
ping_test "联通" "$ip"
done
echo -e "\n ${BOLD}▶ 移动${RESET}"
for ip in "${MOBILE_IPS[@]}"; do
ping_test "移动" "$ip"
done
}
# ── 回程路由追踪 ──────────────────────────────────────────────
return_route_nexttrace() {
local label=$1
local ip=$2
local color=$3
echo -e "\n ${color}${BOLD}${label} 回程路由 → ${ip}${RESET}"
echo -e " ${CYAN}$(printf '─%.0s' {1..48})${RESET}"
if command -v nexttrace &>/dev/null; then
nexttrace --no-rdns "$ip" 2>/dev/null | \
grep -v "^$" | \
sed 's/^/ /'
else
echo -e " ${RED}nexttrace 未安装,使用 traceroute 替代无ASN信息${RESET}"
traceroute -n -m 20 "$ip" 2>/dev/null | sed 's/^/ /'
fi
}
return_route_test() {
print_section "📥 回程路由追踪VPS → 国内三网)"
echo -e " ${YELLOW}提示: 关注 AS 号码${RESET}"
echo -e " ${YELLOW} 电信CN2: as4809 普通163: as4134${RESET}"
echo -e " ${YELLOW} 联通9929: as9929 普通169: as4837${RESET}"
echo -e " ${YELLOW} 移动CMIN2: as58807 普通CMNet: as9808${RESET}"
return_route_nexttrace "电信" "$TELECOM_RETURN" "$RED"
return_route_nexttrace "联通" "$UNICOM_RETURN" "$BLUE"
return_route_nexttrace "移动" "$MOBILE_RETURN" "$GREEN"
}
# ── MTR 质量测试 ──────────────────────────────────────────────
mtr_test() {
print_section "📊 MTR 丢包率测试各发送10个包"
local targets=(
"电信上海|$TELECOM_RETURN"
"联通武汉|$UNICOM_RETURN"
"移动北京|$MOBILE_RETURN"
)
for item in "${targets[@]}"; do
local label="${item%%|*}"
local ip="${item##*|}"
echo -e "\n ${BOLD}${label} (${ip})${RESET}"
if command -v mtr &>/dev/null; then
mtr --report --report-cycles 10 --no-dns "$ip" 2>/dev/null | \
awk 'NR>1 {printf " %-5s %-18s Loss:%5s Avg:%7s ms\n", $1, $2, $3, $8}' | \
head -20
else
echo -e " ${RED}mtr 未安装${RESET}"
fi
done
}
# ── 摘要 ──────────────────────────────────────────────────────
summary() {
print_section "✅ 检测完成"
echo -e " 如需可视化路由图,访问 nexttrace 输出的 MapTrace URL"
echo -e " 如需重新测试: ${CYAN}bash route_test.sh${RESET}"
echo ""
}
# ── 主流程 ────────────────────────────────────────────────────
main() {
check_root
print_banner
get_vps_info
install_deps
outbound_test
return_route_test
mtr_test
summary
}
main