AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 457295
Accepted
aef
aef
Asked: 2012-12-12 19:13:52 +0800 CST2012-12-12 19:13:52 +0800 CST 2012-12-12 19:13:52 +0800 CST

在 ControlMaster SSH 会话中以编程方式删除端口转发

  • 772

不久前,我得到了一个答案,告诉我如何在正在运行的 SSH ControlMaster 进程上添加端口转发。知道这很有帮助,但我仍然缺少一种方法来删除这样的端口转发,因为我不再需要它了。

据我所知,您可以通过正常连接上的内部命令键序列来做到这一点,这似乎对 ControlMaster 客户端是禁用的。即使那是可能的,我也需要一个可以用脚本自动化的解决方案,这肯定不是那么容易。

有办法吗?它是否易于自动化?

security
  • 1 1 个回答
  • 3640 Views

1 个回答

  • Voted
  1. Best Answer
    aculich
    2012-12-12T22:41:44+08:002012-12-12T22:41:44+08:00

    正如我之前的回答说明,ssh 手册页-O ctl_cmd详细解释了、、或ctl_cmd之一。checkforwardcancelexit

    -O ctl_cmd
            Control an active connection multiplexing master process.  When the -O option is
            specified, the ctl_cmd argument is interpreted and passed to the master process.
            Valid commands are: “check” (check that the master process is running), “forward”
            (request forwardings without command execution), “exit” (request the master to
            exit), and “stop” (request the master to stop accepting further multiplexing
            requests).
    

    您可以使用ctl_cmd来完成您想要的脚本自动化。您需要先创建一个ControlMaster套接字,然后-S像这样连接到远程主机:

    ssh -v -M -S/tmp/controlmaster-remotehost remotehost
    

    然后你可以从你的本地机器forward和cancel你喜欢的端口。您可以使用一条命令转发多个端口:

    ssh -O forward -S/tmp/controlmaster-remotehost -L5555:localhost:22 -L3333:localhost:22 remotehost
    

    和/或一次处理一个:

    ssh -O cancel  -S/tmp/controlmaster-remotehost -L5555:localhost:22 remotehost
    ssh -O cancel  -S/tmp/controlmaster-remotehost -L3333:localhost:22 remotehost
    

    我通过并排会话创建了一个要点,显示了ssh -O ctl_cmd实际操作;左侧是本地主机的端口forward/ cancel,右侧是​​连接到远程主机的 ssh 输出:

    https://gist.github.com/raw/4265549/999d90b8f85190a41eed00e4d60d5d22da6c67b9/ssh-controlmaster-side-by-side.log

    这些命令仅从OpenSSH 6.0开始可用:

     * ssh(1): support for cancelling local and remote port forwards via the
       multiplex socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host"
       to request the cancellation of the specified forwardings
     * support cancellation of local/dynamic forwardings from ~C commandline
    

    如果您有较早的版本,则需要升级。如果您使用的是 Mac OS X,则可以安装 macports,然后使用升级:sudo port install openssh这会将其安装到/opt/local/bin.

    • 6

相关问题

  • OpenSSH 漏洞 [重复]

  • 选择什么安全套件?

  • 安全地授予对 SQL 2005 复制监视器的访问权限以创建快照

  • SSH 服务器零日漏洞利用 - 保护自己的建议

  • 如何将安全组添加到正在运行的 EC2 实例?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve