Update 02

This commit is contained in:
2025-11-13 14:34:19 +08:00
committed by GitHub
parent f232183388
commit 14565de26a

379
02
View File

@@ -1,8 +1,6 @@
#!/bin/bash #!/bin/bash
# 一键网络检测脚本 # 修复版一键网络检测脚本
# 功能:自动安装依赖 + 多方案检测 + 详细报告
set -e set -e
# 颜色定义 # 颜色定义
@@ -15,111 +13,65 @@ CYAN='\033[0;36m'
NC='\033[0m' NC='\033[0m'
# 日志函数 # 日志函数
log() { log() { echo -e "${BLUE}[$(date '+%H:%M:%S')]${NC} $1"; }
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"; }
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() { install_dependencies() {
log "检查并安装必要的网络工具..." log "检查并安装必要的网络工具..."
local tools=("curl" "wget" "netcat" "telnet" "nmap" "traceroute") local tools_missing=()
local to_install=()
# 检查缺失的工具 # 检查工具是否存在
for tool in "${tools[@]}"; do if ! command -v nc &> /dev/null && ! command -v netcat &> /dev/null; then
if ! command -v "$tool" &> /dev/null; then tools_missing+=("netcat-openbsd")
to_install+=("$tool") fi
if ! command -v telnet &> /dev/null; then
tools_missing+=("telnet")
fi
if ! command -v nmap &> /dev/null; then
tools_missing+=("nmap")
fi
if ! command -v traceroute &> /dev/null; then
tools_missing+=("traceroute")
fi
if ! command -v curl &> /dev/null; then
tools_missing+=("curl")
fi fi
done
if [ ${#to_install[@]} -eq 0 ]; then if [ ${#tools_missing[@]} -eq 0 ]; then
success "所有必要工具已安装" success "所有必要工具已安装"
return 0 return 0
fi fi
info "需要安装的工具: ${to_install[*]}" info "需要安装的工具: ${tools_missing[*]}"
case $OS in # Debian/Ubuntu系统
ubuntu|debian) if command -v apt &> /dev/null; then
apt update apt update
apt install -y "${to_install[@]}" if apt install -y "${tools_missing[@]}"; then
;;
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 "工具安装完成" success "工具安装完成"
}
# 基础网络检测
basic_network_check() {
log "执行基础网络检测..."
# 检测DNS
if nslookup google.com &> /dev/null; then
success "DNS解析正常"
else else
error "DNS解析失败" # 如果批量安装失败,尝试逐个安装
fi for tool in "${tools_missing[@]}"; do
if apt install -y "$tool"; then
# 检测网关 success "安装 $tool 成功"
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 else
error "网关无法连通" warning "安装 $tool 失败,跳过"
fi fi
done
fi fi
# CentOS/RHEL系统
# 检测外网 elif command -v yum &> /dev/null; then
if ping -c 2 -W 3 8.8.8.8 &> /dev/null; then yum install -y "${tools_missing[@]}"
success "外网连通性正常" elif command -v dnf &> /dev/null; then
dnf install -y "${tools_missing[@]}"
else else
error "外网无法连通" warning "未知包管理器,请手动安装工具"
return 1
fi fi
} }
@@ -127,42 +79,63 @@ basic_network_check() {
check_port() { check_port() {
local host=$1 local host=$1
local port=$2 local port=$2
local timeout=$3 local timeout=${3:-5}
log "检测 $host:$port ..." log "检测 $host:$port (超时: ${timeout}s)"
# 方法1: nc local methods=0
local success_methods=0
# 方法1: nc (netcat)
if command -v nc &> /dev/null; then if command -v nc &> /dev/null; then
((methods++))
if timeout "$timeout" nc -z -w "$timeout" "$host" "$port" &> /dev/null; then if timeout "$timeout" nc -z -w "$timeout" "$host" "$port" &> /dev/null; then
success "nc检测: 端口 $port 开放" success "nc检测: 端口 $port 开放"
return 0 ((success_methods++))
else
error "nc检测: 端口 $port 关闭"
fi fi
fi fi
# 方法2: telnet # 方法2: /dev/tcp (bash内置)
if command -v telnet &> /dev/null; then ((methods++))
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 if timeout "$timeout" bash -c "echo > /dev/tcp/$host/$port" &> /dev/null; then
success "bash检测: 端口 $port 开放" success "bash检测: 端口 $port 开放"
return 0 ((success_methods++))
else
error "bash检测: 端口 $port 关闭"
fi
# 方法3: telnet
if command -v telnet &> /dev/null; then
((methods++))
if echo "quit" | timeout "$timeout" telnet "$host" "$port" 2>&1 | grep -q "Connected\|Escape character"; then
success "telnet检测: 端口 $port 开放"
((success_methods++))
else
error "telnet检测: 端口 $port 关闭"
fi
fi fi
# 方法4: nmap # 方法4: nmap
if command -v nmap &> /dev/null; then if command -v nmap &> /dev/null; then
((methods++))
if nmap -p "$port" "$host" 2>&1 | grep -q "$port/tcp open"; then if nmap -p "$port" "$host" 2>&1 | grep -q "$port/tcp open"; then
success "nmap检测: 端口 $port 开放" success "nmap检测: 端口 $port 开放"
return 0 ((success_methods++))
else
error "nmap检测: 端口 $port 关闭"
fi fi
fi fi
error "所有方法检测: 端口 $port 关闭" # 汇总结果
if [ $success_methods -gt 0 ]; then
success "端口检测结果: $success_methods/$methods 种方法确认端口开放"
return 0
else
error "端口检测结果: 所有 $methods 种方法确认端口关闭"
return 1 return 1
fi
} }
# HTTP服务检测 # HTTP服务检测
@@ -175,12 +148,23 @@ check_http_service() {
# 尝试HTTP # 尝试HTTP
if curl -s -I --connect-timeout 5 "http://$host:$port/" &> /dev/null; then if curl -s -I --connect-timeout 5 "http://$host:$port/" &> /dev/null; then
success "HTTP服务正常 (http://$host:$port)" success "HTTP服务正常 (http://$host:$port)"
# 获取HTTP头信息
echo "HTTP响应头:"
curl -s -I --connect-timeout 3 "http://$host:$port/" | head -10
return 0 return 0
fi fi
# 尝试HTTPS # 尝试HTTPS
if curl -s -I --connect-timeout 5 "https://$host:$port/" &> /dev/null; then if curl -s -I --connect-timeout 5 "https://$host:$port/" &> /dev/null; then
success "HTTPS服务正常 (https://$host:$port)" success "HTTPS服务正常 (https://$host:$port)"
echo "HTTPS响应头:"
curl -s -I --connect-timeout 3 "https://$host:$port/" | head -10
return 0
fi
# 尝试简单的TCP连接测试
if timeout 3 bash -c "echo -e 'GET / HTTP/1.0\r\n\r\n' | nc $host $port" 2>/dev/null | head -1 | grep -q "HTTP"; then
success "HTTP服务响应 (手动测试)"
return 0 return 0
fi fi
@@ -192,14 +176,14 @@ check_http_service() {
trace_route() { trace_route() {
local host=$1 local host=$1
log "执行路由跟踪..." log "执行路由跟踪到 $host ..."
if command -v traceroute &> /dev/null; then if command -v traceroute &> /dev/null; then
traceroute -w 1 -q 1 -m 15 "$host" | head -20 traceroute -w 1 -q 1 -m 10 "$host" 2>/dev/null | head -15
elif command -v tracepath &> /dev/null; then elif command -v tracepath &> /dev/null; then
tracepath "$host" | head -10 tracepath "$host" 2>/dev/null | head -10
else else
warning "未找到路由跟踪工具" warning "未找到路由跟踪工具,跳过"
fi fi
} }
@@ -207,139 +191,120 @@ trace_route() {
scan_common_ports() { scan_common_ports() {
local host=$1 local host=$1
log "扫描常用端口..." 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) local common_ports=(21 22 23 53 80 110 143 443 465 587 993 995 1433 1521 3306 3389 5432 5900 6379 27017 21114)
local open_ports=()
for port in "${common_ports[@]}"; do for port in "${common_ports[@]}"; do
if timeout 2 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null; then if timeout 1 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null; then
open_ports+=("$port")
success "端口 $port 开放" success "端口 $port 开放"
fi fi
done done
if [ ${#open_ports[@]} -gt 0 ]; then
success "发现 ${#open_ports[@]} 个开放端口: ${open_ports[*]}"
else
warning "未发现常用开放端口"
fi
} }
# 显示网络信息 # 主检测函数
show_network_info() { main_check() {
log "系统网络信息:" local target=$1
local port=$2
local timeout=$3
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 -e "${PURPLE}"
echo "==========================================" echo "=========================================="
echo " 一键网络检测脚本" echo " 网络端口检测报告"
echo "==========================================" echo "=========================================="
echo -e "${NC}" echo -e "${NC}"
# 参数处理 log "目标: $target"
local target_host="" [ -n "$port" ] && log "端口: $port"
local target_port="" log "时间: $(date)"
echo
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 install_dependencies
echo
log "开始网络检测..."
echo echo
# 显示本地网络信息 # 基础连通性检查
show_network_info log "基础网络连通性检查..."
if ping -c 2 -W 2 "$target" &> /dev/null; then
success "主机网络可达"
else
error "主机网络不可达"
fi
echo echo
# 基础网络检测
basic_network_check
echo
# 如果指定了目标主机
if [ -n "$target_host" ]; then
# 路由跟踪 # 路由跟踪
trace_route "$target_host" trace_route "$target"
echo echo
# 批量端口扫描 # 批量端口扫描
scan_common_ports "$target_host" scan_common_ports "$target"
echo echo
# 如果指定了端口 # 如果指定了端口,进行详细检测
if [ -n "$target_port" ]; then if [ -n "$port" ]; then
check_port "$target_host" "$target_port" 5 log "开始详细端口检测..."
check_port "$target" "$port" "$timeout"
echo echo
check_http_service "$target_host" "$target_port"
echo
fi
# 交互式端口检测 # HTTP服务检测如果是Web端口
info "是否进行自定义端口检测? (y/n)" if [[ "$port" =~ ^(80|443|8080|8443)$ ]]; then
read -r choice check_http_service "$target" "$port"
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
fi fi
echo echo
success "网络检测完成!" success "检测完成!"
info "检测报告生成,请查看上方结果" info "报告生成时间: $(date)"
} }
# 脚本帮助 # 使用说明
show_help() { show_usage() {
echo "用法: $0 [目标地址] [端口]" echo "用法: $0 [目标地址] [端口] [超时时间]"
echo echo
echo "示例:" echo "示例:"
echo " $0 # 交互式检测" echo " $0 27.194.150.137 21114 # 检测指定IP和端口"
echo " $0 google.com # 检测指定域名" echo " $0 google.com 80 10 # 检测域名端口超时10秒"
echo " $0 192.168.1.1 80 # 检测指定IP和端口" echo " $0 192.168.1.1 # 只扫描常用端口"
echo " $0 example.com 443 # 检测HTTPS服务"
echo echo
echo "功能:" echo "注意:"
echo " - 自动安装网络检测工具" echo " - 脚本会自动安装必要的检测工具"
echo " - 基础网络连通性检测" echo " - 超时时间默认5秒"
echo " - 端口扫描和服务检测"
echo " - 路由跟踪和网络诊断"
} }
# 参数处理 # 参数解析
case "${1:-}" in if [ $# -eq 0 ]; then
-h|--help|help) info "请输入要检测的目标地址:"
show_help read -r target_input
exit 0
;; # 解析输入(支持 host:port 格式)
*) if [[ "$target_input" =~ : ]]; then
main "$@" target_host=$(echo "$target_input" | cut -d: -f1)
;; target_port=$(echo "$target_input" | cut -d: -f2)
esac info "请输入超时时间(默认5秒):"
read -r timeout_input
timeout=${timeout_input:-5}
else
target_host="$target_input"
info "请输入要检测的端口(直接回车只扫描常用端口):"
read -r target_port
if [ -n "$target_port" ]; then
info "请输入超时时间(默认5秒):"
read -r timeout_input
timeout=${timeout_input:-5}
fi
fi
else
target_host=$1
target_port=$2
timeout=${3:-5}
fi
# 运行检测
main_check "$target_host" "$target_port" "$timeout"