#!/bin/bash # 多方案端口检测脚本 # 用法: ./port_check.sh IP地址 端口号 [超时时间] TARGET_HOST="${1}" TARGET_PORT="${2}" TIMEOUT="${3:-5}" # 默认超时5秒 # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # 日志函数 log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # 检查依赖工具 check_dependencies() { local tools=("nc" "telnet" "curl" "timeout" "nmap") local missing=() for tool in "${tools[@]}"; do if ! command -v "$tool" &> /dev/null; then missing+=("$tool") fi done if [ ${#missing[@]} -gt 0 ]; then log_warning "以下工具未安装: ${missing[*]}" log_info "部分检测功能可能受限" fi } # 方案1: 使用nc (netcat) 检测端口 check_with_nc() { log_info "方案1: 使用 netcat 检测端口..." if command -v nc &> /dev/null; then if timeout "$TIMEOUT" nc -z -w "$TIMEOUT" "$TARGET_HOST" "$TARGET_PORT" &> /dev/null; then log_success "nc检测: 端口 $TARGET_PORT 可达" return 0 else log_error "nc检测: 端口 $TARGET_PORT 不可达" return 1 fi else log_warning "nc 未安装,跳过此方案" return 2 fi } # 方案2: 使用telnet检测端口 check_with_telnet() { log_info "方案2: 使用 telnet 检测端口..." if command -v telnet &> /dev/null; then # 创建telnet命令脚本 local telnet_cmd="echo 'quit' | timeout $TIMEOUT telnet $TARGET_HOST $TARGET_PORT" if eval "$telnet_cmd" 2>&1 | grep -q "Connected\|Escape character"; then log_success "telnet检测: 端口 $TARGET_PORT 可达" return 0 else log_error "telnet检测: 端口 $TARGET_PORT 不可达" return 1 fi else log_warning "telnet 未安装,跳过此方案" return 2 fi } # 方案3: 使用curl检测HTTP/HTTPS端口 check_with_curl() { log_info "方案3: 使用 curl 检测HTTP服务..." if command -v curl &> /dev/null; then local protocols=("http" "https") for proto in "${protocols[@]}"; do log_info "尝试 $proto 协议..." if timeout "$TIMEOUT" curl -s -I "${proto}://${TARGET_HOST}:${TARGET_PORT}/" &> /dev/null; then log_success "curl检测: $proto 服务在端口 $TARGET_PORT 可达" return 0 fi done log_error "curl检测: HTTP/HTTPS服务在端口 $TARGET_PORT 不可达" return 1 else log_warning "curl 未安装,跳过此方案" return 2 fi } # 方案4: 使用nmap检测端口 check_with_nmap() { log_info "方案4: 使用 nmap 检测端口..." if command -v nmap &> /dev/null; then if timeout "$TIMEOUT" nmap -p "$TARGET_PORT" "$TARGET_HOST" 2>&1 | grep -q "$TARGET_PORT/tcp open"; then log_success "nmap检测: 端口 $TARGET_PORT 开放" return 0 else log_error "nmap检测: 端口 $TARGET_PORT 关闭" return 1 fi else log_warning "nmap 未安装,跳过此方案" return 2 fi } # 方案5: 使用/dev/tcp检测 (bash内置) check_with_bash_tcp() { log_info "方案5: 使用 bash /dev/tcp 检测..." if timeout "$TIMEOUT" bash -c "echo > /dev/tcp/$TARGET_HOST/$TARGET_PORT" &> /dev/null; then log_success "bash /dev/tcp检测: 端口 $TARGET_PORT 可达" return 0 else log_error "bash /dev/tcp检测: 端口 $TARGET_PORT 不可达" return 1 fi } # 方案6: 使用ping检测主机可达性 check_ping() { log_info "方案6: 检测主机网络连通性..." if ping -c 2 -W 2 "$TARGET_HOST" &> /dev/null; then log_success "ping检测: 主机 $TARGET_HOST 网络可达" return 0 else log_error "ping检测: 主机 $TARGET_HOST 网络不可达" return 1 fi } # 主检测函数 main_check() { log_info "开始检测 $TARGET_HOST:$TARGET_PORT ..." log_info "超时时间: $TIMEOUT 秒" echo "========================================" # 检查依赖 check_dependencies echo "----------------------------------------" local results=() local methods=("check_with_nc" "check_with_telnet" "check_with_curl" "check_with_nmap" "check_with_bash_tcp") # 先检查主机连通性 if ! check_ping; then log_error "主机网络不可达,可能无法连接端口" fi echo "----------------------------------------" # 执行各种检测方法 for method in "${methods[@]}"; do if $method; then results+=("$method: 成功") else case $? in 1) results+=("$method: 失败") ;; 2) results+=("$method: 跳过") ;; esac fi echo "----------------------------------------" done # 汇总结果 log_info "检测结果汇总:" for result in "${results[@]}"; do echo " - $result" done } # 使用说明 usage() { echo "多方案端口检测脚本" echo "用法: $0 <端口号> [超时时间]" echo "示例:" echo " $0 google.com 80" echo " $0 192.168.1.1 22 10" echo " $0 example.com 443 3" } # 参数检查 if [ $# -lt 2 ]; then usage exit 1 fi # 运行主检测 main_check