Update 02

This commit is contained in:
2025-11-13 14:39:28 +08:00
committed by GitHub
parent 14565de26a
commit 312e0ce2e3

434
02
View File

@@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# 修复版一键网络检测脚本 # 增强版网络检测脚本 - 更多端口扫描
set -e set -e
# 颜色定义 # 颜色定义
@@ -19,183 +19,123 @@ warning() { echo -e "${YELLOW}⚠${NC} $1"; }
error() { echo -e "${RED}✗${NC} $1"; } error() { echo -e "${RED}✗${NC} $1"; }
info() { echo -e "${CYAN}${NC} $1"; } info() { echo -e "${CYAN}${NC} $1"; }
# 安装依赖工具(修复版) # 扩展的常用端口列表
install_dependencies() { get_common_ports() {
log "检查并安装必要的网络工具..." # 返回所有常用端口数组
local ports=(
# SSH和相关
22 2222 22222
local tools_missing=() # 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
# 检查工具是否存在 # 数据库
if ! command -v nc &> /dev/null && ! command -v netcat &> /dev/null; then 3306 5432 27017 6379 9200 9300
tools_missing+=("netcat-openbsd") 1433 1521 2638 3389 5433 5500
fi 27018 27019 28017 5000 5984 11211
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
if [ ${#tools_missing[@]} -eq 0 ]; then # FTP和相关
success "所有必要工具已安装" 21 20 2121 2221
return 0
fi
info "需要安装的工具: ${tools_missing[*]}" # Telnet和相关
23 2323 2333
# Debian/Ubuntu系统 # SMTP/邮件
if command -v apt &> /dev/null; then 25 465 587 110 995 143 993
apt update
if apt install -y "${tools_missing[@]}"; then # DNS和网络服务
success "工具安装完成" 53 67 68 69 123 161 162 389 636
else
# 如果批量安装失败,尝试逐个安装 # HTTP代理
for tool in "${tools_missing[@]}"; do 3128 8080 8118 8123
if apt install -y "$tool"; then
success "安装 $tool 成功" # 游戏服务器
else 25565 27015 7777 7778 2302 2303 2304 2305
warning "安装 $tool 失败,跳过" 28960 27960 3074 3478 4379 4380
fi
done # 媒体服务器
fi 32400 1900 5353 9001 9002 1935 554 8554
# CentOS/RHEL系统
elif command -v yum &> /dev/null; then # 监控和管理
yum install -y "${tools_missing[@]}" 9090 3000 5601 9093 9094 9095
elif command -v dnf &> /dev/null; then 9100 9115 9125 9130 9145 9150
dnf install -y "${tools_missing[@]}"
else # P2P和文件共享
warning "未知包管理器,请手动安装工具" 6881 6882 6883 6884 6885 6886 6887 6888 6889
return 1 51413 4662 4664 4665 4672 6346 6347 6881 6889
fi
# 虚拟化和容器
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[@]}"
} }
# 端口检测函数 # 批量端口扫描(增强版)
check_port() {
local host=$1
local port=$2
local timeout=${3:-5}
log "检测 $host:$port (超时: ${timeout}s)"
local methods=0
local success_methods=0
# 方法1: nc (netcat)
if command -v nc &> /dev/null; then
((methods++))
if timeout "$timeout" nc -z -w "$timeout" "$host" "$port" &> /dev/null; then
success "nc检测: 端口 $port 开放"
((success_methods++))
else
error "nc检测: 端口 $port 关闭"
fi
fi
# 方法2: /dev/tcp (bash内置)
((methods++))
if timeout "$timeout" bash -c "echo > /dev/tcp/$host/$port" &> /dev/null; then
success "bash检测: 端口 $port 开放"
((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
# 方法4: nmap
if command -v nmap &> /dev/null; then
((methods++))
if nmap -p "$port" "$host" 2>&1 | grep -q "$port/tcp open"; then
success "nmap检测: 端口 $port 开放"
((success_methods++))
else
error "nmap检测: 端口 $port 关闭"
fi
fi
# 汇总结果
if [ $success_methods -gt 0 ]; then
success "端口检测结果: $success_methods/$methods 种方法确认端口开放"
return 0
else
error "端口检测结果: 所有 $methods 种方法确认端口关闭"
return 1
fi
}
# 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)"
# 获取HTTP头信息
echo "HTTP响应头:"
curl -s -I --connect-timeout 3 "http://$host:$port/" | head -10
return 0
fi
# 尝试HTTPS
if curl -s -I --connect-timeout 5 "https://$host:$port/" &> /dev/null; then
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
fi
error "HTTP/HTTPS服务无法访问"
return 1
}
# 路由跟踪
trace_route() {
local host=$1
log "执行路由跟踪到 $host ..."
if command -v traceroute &> /dev/null; then
traceroute -w 1 -q 1 -m 10 "$host" 2>/dev/null | head -15
elif command -v tracepath &> /dev/null; then
tracepath "$host" 2>/dev/null | head -10
else
warning "未找到路由跟踪工具,跳过"
fi
}
# 批量端口扫描
scan_common_ports() { scan_common_ports() {
local host=$1 local host=$1
local batch_size=${2:-50} # 每批扫描的端口数量
log "快速扫描常用端口..." log "扩展扫描常用端口 (批量大小: $batch_size)..."
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 all_ports=($(get_common_ports))
local total_ports=${#all_ports[@]}
local open_ports=() local open_ports=()
local current_batch=()
for port in "${common_ports[@]}"; do 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 if timeout 1 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null; then
open_ports+=("$port") open_ports+=("$port")
success "端口 $port 开放" success "端口 $port 开放"
@@ -203,9 +143,66 @@ scan_common_ports() {
done done
if [ ${#open_ports[@]} -gt 0 ]; then if [ ${#open_ports[@]} -gt 0 ]; then
success "发现 ${#open_ports[@]} 个开放端口: ${open_ports[*]}" success "发现 ${#open_ports[@]} 个开放端口"
echo "开放端口列表: ${open_ports[*]}"
else else
warning "未发现常用开放端口" 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 fi
} }
@@ -213,69 +210,98 @@ scan_common_ports() {
main_check() { main_check() {
local target=$1 local target=$1
local port=$2 local port=$2
local timeout=$3
echo -e "${PURPLE}" echo -e "${PURPLE}"
echo "==========================================" echo "=========================================="
echo " 网络端口检测报告" echo " 增强版网络端口检测"
echo "==========================================" echo "=========================================="
echo -e "${NC}" echo -e "${NC}"
log "目标: $target" log "目标: $target"
[ -n "$port" ] && log "端口: $port" [ -n "$port" ] && log "指定端口: $port"
log "时间: $(date)" log "时间: $(date)"
echo echo
# 安装依赖
install_dependencies
echo
# 基础连通性检查 # 基础连通性检查
log "基础网络连通性检查..." log "基础网络连通性检查..."
if ping -c 2 -W 2 "$target" &> /dev/null; then if ping -c 2 -W 2 "$target" &> /dev/null; then
success "主机网络可达" success "主机网络可达"
else else
error "主机网络不可达" warning "主机ICMP不可达可能被防火墙阻止继续端口检测..."
fi fi
echo echo
# 路由跟踪 # 路由跟踪
trace_route "$target" 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 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" scan_common_ports "$target"
echo ;;
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 if [ -n "$port" ]; then
log "开始详细端口检测..." log "检测指定端口: $port"
check_port "$target" "$port" "$timeout" 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 echo
# HTTP服务检测如果是Web端口
if [[ "$port" =~ ^(80|443|8080|8443)$ ]]; then
check_http_service "$target" "$port"
fi
fi
echo
success "检测完成!" success "检测完成!"
info "报告生成时间: $(date)" info "报告生成时间: $(date)"
} }
# 使用说明 # 使用说明
show_usage() { show_usage() {
echo "用法: $0 [目标地址] [端口] [超时时间]" echo "用法: $0 [目标地址] [端口]"
echo echo
echo "示例:" echo "示例:"
echo " $0 27.194.150.137 21114 # 检测指定IP和端口" echo " $0 27.194.150.137 # 交互式扫描"
echo " $0 google.com 80 10 # 检测域名端口超时10秒" echo " $0 27.194.150.137 21114 # 检测指定端口"
echo " $0 192.168.1.1 # 只扫描常用端口"
echo echo
echo "注意:" echo "特点:"
echo " - 脚本会自动安装必要的检测工具" echo " - 支持200+个常用端口扫描"
echo " - 超时时间默认5秒" echo " - 多种扫描模式可选"
echo " - 支持端口范围扫描"
} }
# 参数解析 # 参数解析
@@ -283,28 +309,18 @@ if [ $# -eq 0 ]; then
info "请输入要检测的目标地址:" info "请输入要检测的目标地址:"
read -r target_input read -r target_input
# 解析输入(支持 host:port 格式)
if [[ "$target_input" =~ : ]]; then if [[ "$target_input" =~ : ]]; then
target_host=$(echo "$target_input" | cut -d: -f1) target_host=$(echo "$target_input" | cut -d: -f1)
target_port=$(echo "$target_input" | cut -d: -f2) target_port=$(echo "$target_input" | cut -d: -f2)
info "请输入超时时间(默认5秒):"
read -r timeout_input
timeout=${timeout_input:-5}
else else
target_host="$target_input" target_host="$target_input"
info "请输入要检测的端口(直接回车只扫描常用端口):" info "请输入要检测的端口(直接回车进行端口扫描):"
read -r target_port read -r target_port
if [ -n "$target_port" ]; then
info "请输入超时时间(默认5秒):"
read -r timeout_input
timeout=${timeout_input:-5}
fi
fi fi
else else
target_host=$1 target_host=$1
target_port=$2 target_port=$2
timeout=${3:-5}
fi fi
# 运行检测 # 运行检测
main_check "$target_host" "$target_port" "$timeout" main_check "$target_host" "$target_port"