Files
dock/02
2025-11-13 14:29:03 +08:00

346 lines
8.0 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
# 一键网络检测脚本
# 功能:自动安装依赖 + 多方案检测 + 详细报告
set -e
# 颜色定义
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'
# 日志函数
log() {
echo -e "${BLUE}[$(date '+%H:%M:%S')]${NC} $1"
}
success() {
echo -e "${GREEN}${NC} $1"
}
warning() {
echo -e "${YELLOW}${NC} $1"
}
error() {
echo -e "${RED}${NC} $1"
}
info() {
echo -e "${CYAN}${NC} $1"
}
# 检查系统类型
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
else
OS=$(uname -s)
fi
log "检测到系统: $OS"
}
# 安装依赖工具
install_dependencies() {
log "检查并安装必要的网络工具..."
local tools=("curl" "wget" "netcat" "telnet" "nmap" "traceroute")
local to_install=()
# 检查缺失的工具
for tool in "${tools[@]}"; do
if ! command -v "$tool" &> /dev/null; then
to_install+=("$tool")
fi
done
if [ ${#to_install[@]} -eq 0 ]; then
success "所有必要工具已安装"
return 0
fi
info "需要安装的工具: ${to_install[*]}"
case $OS in
ubuntu|debian)
apt update
apt install -y "${to_install[@]}"
;;
centos|rhel|fedora)
if command -v dnf &> /dev/null; then
dnf install -y "${to_install[@]}"
else
yum install -y "${to_install[@]}"
fi
;;
alpine)
apk add "${to_install[@]}"
;;
*)
warning "未知系统,请手动安装以下工具: ${to_install[*]}"
return 1
;;
esac
success "工具安装完成"
}
# 基础网络检测
basic_network_check() {
log "执行基础网络检测..."
# 检测DNS
if nslookup google.com &> /dev/null; then
success "DNS解析正常"
else
error "DNS解析失败"
fi
# 检测网关
if ip route show default &> /dev/null; then
local gateway=$(ip route show default | awk '/default/ {print $3}')
success "默认网关: $gateway"
# ping网关
if ping -c 2 -W 1 "$gateway" &> /dev/null; then
success "网关连通性正常"
else
error "网关无法连通"
fi
fi
# 检测外网
if ping -c 2 -W 3 8.8.8.8 &> /dev/null; then
success "外网连通性正常"
else
error "外网无法连通"
fi
}
# 端口检测函数
check_port() {
local host=$1
local port=$2
local timeout=$3
log "检测 $host:$port ..."
# 方法1: nc
if command -v nc &> /dev/null; then
if timeout "$timeout" nc -z -w "$timeout" "$host" "$port" &> /dev/null; then
success "nc检测: 端口 $port 开放"
return 0
fi
fi
# 方法2: telnet
if command -v telnet &> /dev/null; then
if echo "quit" | timeout "$timeout" telnet "$host" "$port" 2>&1 | grep -q "Connected\|Escape character"; then
success "telnet检测: 端口 $port 开放"
return 0
fi
fi
# 方法3: /dev/tcp
if timeout "$timeout" bash -c "echo > /dev/tcp/$host/$port" &> /dev/null; then
success "bash检测: 端口 $port 开放"
return 0
fi
# 方法4: nmap
if command -v nmap &> /dev/null; then
if nmap -p "$port" "$host" 2>&1 | grep -q "$port/tcp open"; then
success "nmap检测: 端口 $port 开放"
return 0
fi
fi
error "所有方法检测: 端口 $port 关闭"
return 1
}
# HTTP服务检测
check_http_service() {
local host=$1
local port=$2
log "检测HTTP服务..."
# 尝试HTTP
if curl -s -I --connect-timeout 5 "http://$host:$port/" &> /dev/null; then
success "HTTP服务正常 (http://$host:$port)"
return 0
fi
# 尝试HTTPS
if curl -s -I --connect-timeout 5 "https://$host:$port/" &> /dev/null; then
success "HTTPS服务正常 (https://$host:$port)"
return 0
fi
error "HTTP/HTTPS服务无法访问"
return 1
}
# 路由跟踪
trace_route() {
local host=$1
log "执行路由跟踪..."
if command -v traceroute &> /dev/null; then
traceroute -w 1 -q 1 -m 15 "$host" | head -20
elif command -v tracepath &> /dev/null; then
tracepath "$host" | head -10
else
warning "未找到路由跟踪工具"
fi
}
# 批量端口扫描
scan_common_ports() {
local host=$1
log "扫描常用端口..."
local common_ports=(21 22 23 25 53 80 110 143 443 465 587 993 995 1433 1521 3306 3389 5432 5900 6379 27017)
for port in "${common_ports[@]}"; do
if timeout 2 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null; then
success "端口 $port 开放"
fi
done
}
# 显示网络信息
show_network_info() {
log "系统网络信息:"
echo "=== IP地址 ==="
ip addr show | grep -E "inet |inet6 " | grep -v "127.0.0.1" || true
echo "=== 路由表 ==="
ip route show | head -10
echo "=== 监听端口 ==="
ss -tulpn | head -20
}
# 主函数
main() {
echo -e "${PURPLE}"
echo "=========================================="
echo " 一键网络检测脚本"
echo "=========================================="
echo -e "${NC}"
# 参数处理
local target_host=""
local target_port=""
if [ $# -ge 1 ]; then
target_host=$1
fi
if [ $# -ge 2 ]; then
target_port=$2
fi
# 如果没有指定目标,使用交互式输入
if [ -z "$target_host" ]; then
info "请输入要检测的目标地址:"
read -r target_host
fi
# 系统检测和依赖安装
detect_os
install_dependencies
echo
log "开始网络检测..."
echo
# 显示本地网络信息
show_network_info
echo
# 基础网络检测
basic_network_check
echo
# 如果指定了目标主机
if [ -n "$target_host" ]; then
# 路由跟踪
trace_route "$target_host"
echo
# 批量端口扫描
scan_common_ports "$target_host"
echo
# 如果指定了端口
if [ -n "$target_port" ]; then
check_port "$target_host" "$target_port" 5
echo
check_http_service "$target_host" "$target_port"
echo
fi
# 交互式端口检测
info "是否进行自定义端口检测? (y/n)"
read -r choice
if [ "$choice" = "y" ] || [ "$choice" = "Y" ]; then
while true; do
info "请输入要检测的端口 (输入 'q' 退出):"
read -r custom_port
if [ "$custom_port" = "q" ]; then
break
fi
if [[ "$custom_port" =~ ^[0-9]+$ ]]; then
check_port "$target_host" "$custom_port" 5
echo
else
error "端口号必须是数字"
fi
done
fi
fi
echo
success "网络检测完成!"
info "检测报告已生成,请查看上方结果"
}
# 脚本帮助
show_help() {
echo "用法: $0 [目标地址] [端口]"
echo
echo "示例:"
echo " $0 # 交互式检测"
echo " $0 google.com # 检测指定域名"
echo " $0 192.168.1.1 80 # 检测指定IP和端口"
echo " $0 example.com 443 # 检测HTTPS服务"
echo
echo "功能:"
echo " - 自动安装网络检测工具"
echo " - 基础网络连通性检测"
echo " - 端口扫描和服务检测"
echo " - 路由跟踪和网络诊断"
}
# 参数处理
case "${1:-}" in
-h|--help|help)
show_help
exit 0
;;
*)
main "$@"
;;
esac