#!/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"; } # 扩展的常用端口列表 get_common_ports() { # 返回所有常用端口数组 local ports=( # SSH和相关 22 2222 22222 # Web服务 80 443 8080 8443 8000 3000 5000 7000 9000 81 82 83 84 85 86 87 88 89 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8888 8880 8870 8860 8850 8840 8830 8820 8810 8800 # 数据库 3306 5432 27017 6379 9200 9300 1433 1521 2638 3389 5433 5500 27018 27019 28017 5000 5984 11211 # FTP和相关 21 20 2121 2221 # Telnet和相关 23 2323 2333 # SMTP/邮件 25 465 587 110 995 143 993 # DNS和网络服务 53 67 68 69 123 161 162 389 636 # HTTP代理 3128 8080 8118 8123 # 游戏服务器 25565 27015 7777 7778 2302 2303 2304 2305 28960 27960 3074 3478 4379 4380 # 媒体服务器 32400 1900 5353 9001 9002 1935 554 8554 # 监控和管理 9090 3000 5601 9093 9094 9095 9100 9115 9125 9130 9145 9150 # P2P和文件共享 6881 6882 6883 6884 6885 6886 6887 6888 6889 51413 4662 4664 4665 4672 6346 6347 6881 6889 # 虚拟化和容器 2375 2376 2377 2378 2379 2380 6443 10250 10255 8443 9443 10443 11443 12443 13443 14443 15443 # 自定义应用端口 21114 21115 21116 21117 21118 21119 30000 31000 32000 33000 34000 35000 40000 41000 42000 43000 44000 45000 50000 51000 52000 53000 54000 55000 60000 61000 62000 63000 64000 65000 # 其他常见服务 111 135 139 445 548 631 1434 1723 2049 2375 2376 3260 3306 3389 5432 5900 5984 6379 8009 8042 8069 8070 8090 8099 8181 8200 8222 8243 8280 8383 8444 8484 8585 8686 8787 8880 8881 8888 9000 9001 9002 9042 9060 9080 9081 9090 9091 9200 9300 9400 9443 9500 9600 9700 9800 9900 9981 9999 10000 10001 10050 10051 10100 10200 ) echo "${ports[@]}" } # 批量端口扫描(增强版) scan_common_ports() { local host=$1 local batch_size=${2:-50} # 每批扫描的端口数量 log "扩展扫描常用端口 (批量大小: $batch_size)..." local all_ports=($(get_common_ports)) local total_ports=${#all_ports[@]} local open_ports=() local current_batch=() info "总共需要扫描 $total_ports 个端口..." for i in "${!all_ports[@]}"; do local port=${all_ports[$i]} current_batch+=("$port") # 当达到批量大小或是最后一个端口时进行扫描 if [ ${#current_batch[@]} -eq $batch_size ] || [ $((i + 1)) -eq $total_ports ]; then for batch_port in "${current_batch[@]}"; do ( if timeout 1 bash -c "echo > /dev/tcp/$host/$batch_port" 2>/dev/null; then echo "OPEN:$batch_port" fi ) & done wait # 清空当前批次 current_batch=() # 显示进度 local progress=$(( (i + 1) * 100 / total_ports )) echo -ne "扫描进度: $progress% ($((i + 1))/$total_ports)\r" fi done echo # 换行 # 重新扫描获取开放端口(简化版本) log "最终确认开放端口..." open_ports=() for port in "${all_ports[@]}"; do if timeout 1 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null; then open_ports+=("$port") success "端口 $port 开放" fi done if [ ${#open_ports[@]} -gt 0 ]; then success "发现 ${#open_ports[@]} 个开放端口" echo "开放端口列表: ${open_ports[*]}" else warning "未发现开放端口" fi return ${#open_ports[@]} } # 快速端口扫描(只扫描最重要的端口) quick_scan() { local host=$1 log "快速扫描重要端口..." local important_ports=(21 22 23 80 443 8080 8443 3306 5432 27017 6379 3389 5900 21114) local open_ports=() for port in "${important_ports[@]}"; do if timeout 1 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null; then open_ports+=("$port") success "端口 $port 开放" fi done if [ ${#open_ports[@]} -gt 0 ]; then success "快速扫描发现 ${#open_ports[@]} 个开放端口: ${open_ports[*]}" else warning "快速扫描未发现开放端口" fi } # 指定范围端口扫描 range_scan() { local host=$1 local start_port=$2 local end_port=$3 log "扫描端口范围: $start_port-$end_port" local open_ports=() local total=$((end_port - start_port + 1)) local current=0 for port in $(seq $start_port $end_port); do ((current++)) local progress=$((current * 100 / total)) echo -ne "进度: $progress% ($current/$total)\r" if timeout 0.5 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null; then open_ports+=("$port") echo -e "\n${GREEN}✓${NC} 端口 $port 开放" fi done echo # 换行 if [ ${#open_ports[@]} -gt 0 ]; then success "范围扫描发现 ${#open_ports[@]} 个开放端口" echo "开放端口: ${open_ports[*]}" else warning "指定范围内未发现开放端口" fi } # 主检测函数 main_check() { local target=$1 local port=$2 echo -e "${PURPLE}" echo "==========================================" echo " 增强版网络端口检测" echo "==========================================" echo -e "${NC}" log "目标: $target" [ -n "$port" ] && log "指定端口: $port" log "时间: $(date)" echo # 基础连通性检查 log "基础网络连通性检查..." if ping -c 2 -W 2 "$target" &> /dev/null; then success "主机网络可达" else warning "主机ICMP不可达(可能被防火墙阻止,继续端口检测...)" fi echo # 路由跟踪 log "执行路由跟踪..." if command -v traceroute &> /dev/null; then traceroute -w 1 -q 1 -m 8 "$target" 2>/dev/null | head -12 else warning "traceroute 未安装,跳过路由跟踪" fi echo # 扫描模式选择 info "请选择扫描模式:" echo "1) 快速扫描 (重要端口)" echo "2) 全面扫描 (200+ 常用端口)" echo "3) 自定义范围扫描" echo "4) 只检测指定端口" read -p "请输入选择 (1-4, 默认1): " scan_choice scan_choice=${scan_choice:-1} case $scan_choice in 1) quick_scan "$target" ;; 2) scan_common_ports "$target" ;; 3) read -p "请输入起始端口: " start_port read -p "请输入结束端口: " end_port if [[ "$start_port" =~ ^[0-9]+$ ]] && [[ "$end_port" =~ ^[0-9]+$ ]]; then range_scan "$target" "$start_port" "$end_port" else error "端口范围无效,使用快速扫描" quick_scan "$target" fi ;; 4) if [ -n "$port" ]; then log "检测指定端口: $port" if timeout 3 bash -c "echo > /dev/tcp/$target/$port" 2>/dev/null; then success "端口 $port 开放" else error "端口 $port 关闭" fi else error "未指定端口,使用快速扫描" quick_scan "$target" fi ;; *) quick_scan "$target" ;; esac echo success "检测完成!" info "报告生成时间: $(date)" } # 使用说明 show_usage() { echo "用法: $0 [目标地址] [端口]" echo echo "示例:" echo " $0 27.194.150.137 # 交互式扫描" echo " $0 27.194.150.137 21114 # 检测指定端口" echo echo "特点:" echo " - 支持200+个常用端口扫描" echo " - 多种扫描模式可选" echo " - 支持端口范围扫描" } # 参数解析 if [ $# -eq 0 ]; then info "请输入要检测的目标地址:" read -r target_input if [[ "$target_input" =~ : ]]; then target_host=$(echo "$target_input" | cut -d: -f1) target_port=$(echo "$target_input" | cut -d: -f2) else target_host="$target_input" info "请输入要检测的端口(直接回车进行端口扫描):" read -r target_port fi else target_host=$1 target_port=$2 fi # 运行检测 main_check "$target_host" "$target_port"