本文目录导读:

- 使用 netcat (nc) 检测基本连通性
- 更完善的 Python 检测脚本
- 使用 curl 检测 HTTP 端口转发
- 完整的检测脚本(包含多种检测方式)
- PowerShell 版本(Windows)
- 使用示例
使用 netcat (nc) 检测基本连通性
#!/bin/bash
# port_forward_check.sh
HOST="example.com"
PORT=8080
# 使用 timeout 防止长时间阻塞
timeout 5 bash -c "echo > /dev/tcp/$HOST/$PORT" 2>/dev/null && \
echo "端口 $PORT 转发正常" || \
echo "端口 $PORT 转发异常"
更完善的 Python 检测脚本
#!/usr/bin/env python3
# port_forward_detect.py
import socket
import sys
import argparse
from concurrent.futures import ThreadPoolExecutor, as_completed
def check_port_forward(target_host, target_port, local_host='127.0.0.1',
local_port=None, timeout=5):
"""
检测端口转发
"""
try:
# 创建 socket 连接
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
# 如果是本地端口转发检测
if local_port:
result = sock.connect_ex((local_host, local_port))
if result == 0:
print(f"✓ 本地 {local_host}:{local_port} 已监听")
else:
result = sock.connect_ex((target_host, target_port))
if result == 0:
print(f"✓ 端口 {target_host}:{target_port} 转发正常")
else:
print(f"✗ 端口 {target_host}:{target_port} 转发异常")
sock.close()
return result == 0
except socket.timeout:
print(f"✗ 连接超时 - {target_host}:{target_port}")
return False
except socket.error as e:
print(f"✗ 连接错误: {e}")
return False
def detect_forward_type(target_host, target_port, timeout=3):
"""
检测端口转发类型
"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
sock.connect((target_host, target_port))
# 获取远程地址
remote_addr = sock.getpeername()
local_addr = sock.getsockname()
print(f"本地地址: {local_addr[0]}:{local_addr[1]}")
print(f"远程地址: {remote_addr[0]}:{remote_addr[1]}")
# 尝试判断转发类型
if local_addr[0] != '127.0.0.1':
print("⚠ 检测到非本地地址,可能是远程端口转发")
else:
print("ℹ 本地端口转发")
sock.close()
except Exception as e:
print(f"无法检测转发类型: {e}")
def scan_ports(target_host, start_port=1, end_port=1024, threads=50):
"""
扫描端口范围检查转发
"""
open_ports = []
def scan_port(port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((target_host, port))
sock.close()
if result == 0:
return port
except:
pass
return None
with ThreadPoolExecutor(max_workers=threads) as executor:
futures = {executor.submit(scan_port, port): port
for port in range(start_port, end_port + 1)}
for future in as_completed(futures):
result = future.result()
if result:
open_ports.append(result)
print(f"发现开放端口: {result}")
return open_ports
def main():
parser = argparse.ArgumentParser(description='端口转发检测工具')
parser.add_argument('host', help='目标主机')
parser.add_argument('port', type=int, help='目标端口')
parser.add_argument('--local-port', type=int, help='本地端口(可选)')
parser.add_argument('--scan', action='store_true', help='扫描端口范围')
parser.add_argument('--start', type=int, default=1, help='起始端口')
parser.add_argument('--end', type=int, default=1024, help='结束端口')
args = parser.parse_args()
if args.scan:
print(f"扫描 {args.host} 端口范围 {args.start}-{args.end}")
open_ports = scan_ports(args.host, args.start, args.end)
print(f"发现 {len(open_ports)} 个开放端口")
else:
# 检查单个端口
print(f"检测 {args.host}:{args.port}")
result = check_port_forward(args.host, args.port,
local_port=args.local_port)
if result:
detect_forward_type(args.host, args.port)
if __name__ == "__main__":
main()
使用 curl 检测 HTTP 端口转发
#!/bin/bash
# http_port_forward_check.sh
URL="http://example.com"
CHECK_KEY="expected_text"
# 检查 HTTP 端口转发
check_http_forward() {
local response=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "$1")
case $response in
200)
echo "HTTP 转发正常 (状态码: $response)"
;;
301|302|307|308)
redirect_url=$(curl -sI --connect-timeout 5 "$1" | grep -i "location:" | cut -d' ' -f2)
echo "HTTP 转发到: $redirect_url"
;;
*)
echo "HTTP 转发异常 (状态码: $response)"
;;
esac
}
# 检查特定响应内容
check_response_content() {
local content=$(curl -s --connect-timeout 5 "$1")
if echo "$content" | grep -q "$2"; then
echo "响应内容包含期望的关键字"
else
echo "响应内容异常"
fi
}
check_http_forward "$URL"
check_response_content "$URL" "$CHECK_KEY"
完整的检测脚本(包含多种检测方式)
#!/bin/bash
# comprehensive_port_check.sh
TIMEOUT=5
HOSTS=("192.168.1.1" "localhost" "0.0.0.0")
PORTS=(22 80 443 8080 3306)
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检测端口是否可达
check_port_forward() {
local host=$1
local port=$2
if timeout $TIMEOUT bash -c "echo >/dev/tcp/$host/$port" 2>/dev/null; then
return 0
else
return 1
fi
}
# 检测 iptables 端口转发规则
check_iptables_rules() {
log_info "检查 iptables 端口转发规则..."
if command -v iptables &> /dev/null; then
# 检查 NAT 表
if iptables -t nat -L -n | grep -q "DNAT\|REDIRECT"; then
log_warn "发现端口转发规则:"
iptables -t nat -L -n --line-numbers | grep -E "DNAT|REDIRECT"
else
log_info "未发现端口转发规则"
fi
fi
}
# 检测 SSH 端口转发
check_ssh_forward() {
log_info "检查 SSH 端口转发..."
# 检查 SSH 进程
if ps aux | grep -q "[s]sh.*-L\|[s]sh.*-R\|[s]sh.*-D"; then
log_warn "发现 SSH 转发连接:"
ps aux | grep "[s]sh.*-L\|[s]sh.*-R\|[s]sh.*-D"
else
log_info "未发现 SSH 转发"
fi
}
# 检测 socat 转发
check_socat_forward() {
log_info "检查 socat 转发..."
if ps aux | grep -q "[s]ocat"; then
log_warn "发现 socat 进程:"
ps aux | grep "[s]ocat"
fi
}
# 主检测循环
main() {
log_info "开始端口转发检测..."
echo "----------------------------------------"
# 检测本地端口
for host in "${HOSTS[@]}"; do
for port in "${PORTS[@]}"; do
if check_port_forward "$host" "$port"; then
log_info "端口 $host:$port 可达"
fi
done
done
echo "----------------------------------------"
# 系统级检测
check_iptables_rules
echo "----------------------------------------"
check_ssh_forward
echo "----------------------------------------"
check_socat_forward
echo "----------------------------------------"
log_info "检测完成"
}
main
PowerShell 版本(Windows)
# port_forward_check.ps1
param(
[string]$Host = "localhost",
[int]$Port = 80,
[switch]$Scan,
[int]$StartPort = 1,
[int]$EndPort = 1024
)
function Test-PortForward {
param(
[string]$TargetHost,
[int]$TargetPort
)
try {
$tcpClient = New-Object System.Net.Sockets.TcpClient
$result = $tcpClient.BeginConnect($TargetHost, $TargetPort, $null, $null)
$timeout = [System.TimeSpan]::FromSeconds(5)
if ($result.AsyncWaitHandle.WaitOne($timeout)) {
$tcpClient.EndConnect($result) | Out-Null
Write-Host "✓ 端口 $TargetHost`:$TargetPort 转发正常" -ForegroundColor Green
return $true
} else {
Write-Host "✗ 端口 $TargetHost`:$TargetPort 连接超时" -ForegroundColor Red
return $false
}
$tcpClient.Close()
} catch {
Write-Host "✗ 端口 $TargetHost`:$TargetPort 转发异常: $_" -ForegroundColor Red
return $false
}
}
# 主逻辑
if ($Scan) {
Write-Host "扫描 $Host 端口范围 $StartPort-$EndPort" -ForegroundColor Yellow
for ($port = $StartPort; $port -le $EndPort; $port++) {
Test-PortForward -TargetHost $Host -TargetPort $port
}
} else {
Test-PortForward -TargetHost $Host -TargetPort $Port
}
使用示例
# 检查单个端口 python3 port_forward_detect.py example.com 8080 # 扫描端口范围 python3 port_forward_detect.py example.com --scan --start 1 --end 1000 # 使用bash脚本 ./port_forward_check.sh # 完整的检测 ./comprehensive_port_check.sh
这些脚本可以检测:
- 基本的 TCP 端口连通性
- HTTP/HTTPS 端口转发
- iptables 端口转发规则
- SSH 隧道转发
- socat 转发
- 本地和远程端口转发
根据实际需求选择合适的脚本即可。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播