【问题标题】:How to close SSH socket如何关闭 SSH 套接字
【发布时间】:2017-09-20 07:00:27
【问题描述】:

我正在努力在两台服务器之间启用会话多路复用。 我想在创建新的套接字之前关闭该服务器(或 IP)的所有现有套接字,并在完成任务后关闭新创建的套接字。这是我到目前为止所做的:

    remote_ip=192.168.20.2      #User inut
    remote_port=222

可以通过以下方式创建套接字:

    SSHSOCKET=~/.ssh/remote_$remote_ip
    ssh -M -f -N -o ControlPath=$SSHSOCKET $remote_ip -p $remote_port

可以通过以下方式搜索控制路径:

ps x | grep $remote_ip | grep ssh | cut -d '=' -f 2
      /root/.ssh/remote_192.168.20.2 192.168.20.2 -p 222

可以通过以下方式关闭套接字:

ssh -S /root/.ssh/remote_192.168.20.2 192.168.20.2 -p 64555 -O exit

尝试通过以下方式关闭套接字:

ps x | grep $remote_ip | grep ssh | cut -d '=' -f 2 | xargs ssh -S | xargs -i {} "-O exit"

但我明白了:

Pseudo-terminal will not be allocated because stdin is not a terminal.

尝试使用 -t 和 -tt:

        ps x | grep $remote_ip | grep ssh | cut -d '=' -f 2 | xargs ssh -Stt | xargs -i {} "-O exit"
        ssh: Could not resolve hostname /root/.ssh/remote_192.168.20.2: Name or service not known
        xargs: ssh: exited with status 255; aborting

谁能帮我解决这个问题?

【问题讨论】:

  • 你为什么需要分配一个ptty?这是一个警告,而不是错误。您是否有理由不忽略它?
  • 虽然xargs -i {} "-O exit"有自己的问题,与PTY分配无关。
  • ...坦率地说,你为什么要 grepping ps 呢?您可以直接从文件系统中获取套接字。
  • ssh -S /root/.ssh/remote_192.168.20.2 192.168.20.2 -p 64555 -O exit 关闭套接字不会导致错误/警告。但是ps x | grep $remote_ip | grep ssh | cut -d '=' -f 2 | xargs ssh -S | xargs -i {} "-O exit" 没有,尽管它的解释与上面的完全一样。
  • 是的,我可以从文件系统中删除,但我不知道是否存在任何套接字(由其他用户创建)或存储在哪里。这就是我从 ps 搜索的原因,我确信有更好的方法,但到目前为止这是我能想到的最好的方法。

标签: bash ssh xargs


【解决方案1】:

如果您想终止从您的计算机到给定远程 IP 地址和端口的所有连接,您可以执行以下操作(使用 fuser,这是所有主要 Linux 发行版中包含的 psmisc 软件包中的一个工具):

fuser -k -n tcp ",${remote_ip},${remote_port}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多