惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
T
Troy Hunt's Blog
B
Blog
N
Netflix TechBlog - Medium
H
Help Net Security
PCI Perspectives
PCI Perspectives
罗磊的独立博客
SecWiki News
SecWiki News
S
Security Affairs
Webroot Blog
Webroot Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
博客园 - 【当耐特】
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
Hugging Face - Blog
Hugging Face - Blog
Forbes - Security
Forbes - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
M
MIT News - Artificial intelligence
博客园_首页
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
The Cloudflare Blog
P
Proofpoint News Feed
D
DataBreaches.Net
N
News and Events Feed by Topic
G
Google Developers Blog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
AI
AI
O
OpenAI News
雷峰网
雷峰网
C
Check Point Blog
大猫的无限游戏
大猫的无限游戏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队

博客园 - sunny_2016

核聚:雷·达里奥的达成目标5步法 英语发单规则一 英语学习三 英语学习笔记二 英语学习笔记一 modbus协议 OPC UA协议学习笔记 限制ssh非法登录 linux添加hosts.deny文件 jmeter的tcp请求示例发送16进制报文 pymodbus模拟modbus slave从站(二) 使用python的pymodbus实现modbus slave 模拟从站一 secure crt使用ssh密钥登录提示未知文件格式 专业的通讯调试工具或者平台有哪些 centos7.9上面卸载中文语言包和中文字体重新安装 linux centos7.9 中文乱码 linux pkill命令的坑 互联网上的高危IP,一直在尝试sshd破解 通过/etc/hosts.deny限制一个网段的 SSH 登录尝试 linux查询近8小时ssh登录失败的ip转换为hosts.deny格式打印 多系统集成分析——ERP与OA、PLM、MES、CRM、WMS、SRM、HR ERP也有库存管理、生产管理、流程审批,为什么还要上WMS、MES和OA? gitlab流水线执行发布提示: No such file or directory gitlab-runner注册完提示:New runner. Has not connected yet(新的Runner,尚未连接) 电池片组件生产工艺流程
Windows 11 主机上建立反向隧道,实现服务端连接内网客户端主机
sunny_2016 · 2025-12-18 · via 博客园 - sunny_2016

此时需要在 Windows 11 主机上建立反向隧道,将本地端口映射到公网服务器的固定端口。

推荐方法:使用 SSH 反向隧道(Reverse Tunnel)

前提:

  • CentOS 7 开启 SSH 服务(sshd),允许 root 或普通用户登录。
  • Windows 11 安装 OpenSSH 客户端(Win11 默认已含)或使用 PuTTY/plink。

步骤 1:在 CentOS 7 上配置 SSH 允许 GatewayPorts

编辑 /etc/ssh/sshd_config

GatewayPorts yes
AllowTcpForwarding yes

重启 SSH:

这允许远程客户端通过 SSH 将端口绑定到公网 IP(而不仅是 127.0.0.1)。


步骤 2:从 Windows 11 主动建立反向隧道到 CentOS

在 Windows 11 上以管理员身份打开 PowerShell 或 CMD:

# 假设 CentOS 的 SSH 用户为 testuser,IP 为 220.228.253.101
ssh -R 0.0.0.0:502:127.0.0.1:502 \
    -R 0.0.0.0:503:127.0.0.1:503 \
    -R 0.0.0.0:504:127.0.0.1:504 \
    testuser@220.228.253.101

注意:这里假设 Modbus 服务在 Windows 上监听 127.0.0.10.0.0.0。如果是 192.168.3.20,则把 127.0.0.1 改成 192.168.3.20

这样,当连接成功后:

  • 任何访问 220.228.253.101:502 的流量都会被转发到 Windows 11 的 502 端口。
  • 同理适用于 503、504。

方案二:纯 PowerShell + 任务计划程序(无 WSL)

适用于无法使用 WSL 的环境。

步骤 1:创建守护脚本(带重连逻辑)

创建文件:C:\Scripts\modbus_autossh.ps1

# modbus_autossh.ps1
$ErrorActionPreference = "Stop"

$CENTOS_USER = "testuser"
$CENTOS_HOST = "220.228.253.101"
$LOCAL_IP    = "192.168.3.20"
$PORTS       = @(502, 503, 504)

# 日志函数
function Write-Log {
    param([string]$msg)
    $log = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $msg"
    Write-Host $log
    Add-Content -Path "C:\Scripts\tunnel.log" -Value $log
}

# 构建 SSH 参数
$sshArgs = @(
    "-o", "StrictHostKeyChecking=no",
    "-o", "ServerAliveInterval=30",
    "-o", "ServerAliveCountMax=3",
    "-o", "ExitOnForwardFailure=yes",
    "-N"
)

foreach ($port in $PORTs) {
    $sshArgs += "-R", "0.0.0.0:${port}:${LOCAL_IP}:${port}"
}
$sshArgs += "${CENTOS_USER}@${CENTOS_HOST}"

while ($true) {
    Write-Log "Starting SSH tunnel..."
    
    # 启动 SSH 进程(前台阻塞)
    $proc = Start-Process -FilePath "ssh" -ArgumentList $sshArgs -PassThru -Wait
    
    # 如果退出,记录并等待重连
    Write-Log "SSH process exited with code: $($proc.ExitCode)"
    Write-Log "Reconnecting in 10 seconds..."
    Start-Sleep -Seconds 10
}

⚠️ 注意:此脚本会前台运行 SSH(-Wait),确保能捕获退出事件。


步骤 2:创建启动器(用于任务计划程序)

创建:C:\Scripts\start_tunnel.bat

@echo off
cd /d C:\Scripts
powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\modbus_autossh.ps1"

步骤 3:配置任务计划程序

  1. 打开 taskschd.msc
  2. 创建任务:
    • 名称:Modbus AutoSSH (PowerShell)
    • 勾选“不管用户是否登录都要运行”、“使用最高权限”
  3. 触发器:启动时
  4. 操作:启动程序
    • 程序:C:\Scripts\start_tunnel.bat
  5. 条件:取消电源限制
  6. 确定,输入密码

✅ 开机后,脚本将持续运行,断线自动重连。