【问题标题】:Can tmux pane remote ssh-connectiontmux 窗格可以远程 ssh 连接吗
【发布时间】:2013-11-19 04:14:34
【问题描述】:

我可以在本地运行 tmux 并通过 ssh 连接到远程机器吗……然后任何新的窗格和/或屏幕都可以与远程机器外壳一起使用…… 我的意思是我无法在远程计算机上安装 tmux,但我不想从每个窗格都进行 ssh 连接,而是 ssh-login 一次。

这样的事情可能吗.. 谢谢

【问题讨论】:

  • 你可以在远程机器的主目录中安装 tmux 吗?如果没有 tmux,ssh 只会为您提供一个终端。
  • 这是我无法在远程机器上安装任何东西的问题..策略

标签: linux tmux


【解决方案1】:

如果你只想登录一次,你可以使用 ssh 的ControlMaster 功能。将this 之类的配置添加到您的~/.ssh/config

ControlMaster auto
ControlPath /tmp/ssh_mux_%h_%p_%r

如果您(以同一用户)多次登录同一服务器(无论是否在一个 tmux 中),ssh 将重用该连接,因此您无需再次连接和登录。

【讨论】:

    【解决方案2】:

    lilydjwg 解释了一些我以前从未真正理解的东西。了解 ControlMaster 设置会使以下内容更加合理,因为它简化了建立多个 ssh 连接。只需要认证一次,远程主机不需要每次连接都运行一个sshd进程。

    在您的.tmux.conf 文件中:

    # What host do you usually log in to?
    # We'll ssh there by default each time a new window or pane is opened.
    REMOTE_HOST=your.usual.host
    set-option -g default-command "ssh $REMOTE_HOST"
    
    # Simple interface to change which host is connected to when you create
    # a new window or pane.
    bind-key C-h command-prompt -p "Set remote host: " -I $REMOTE_HOST "set-option default-command 'ssh %%'"
    
    # In case you really do want a new window with a local shell.
    bind-key C new-window ""
    

    【讨论】:

      【解决方案3】:

      我不认为tmux 可以。一种解决方法是将这样的内容添加到 tmux.conf

      bind-key X new-window "ssh HOST"
      

      然后新窗口将在远程主机上启动。

      【讨论】:

      • hmm.. 它打开一个新窗口.. 有没有办法只运行命令而不创建新窗口,但留在我执行绑定键命令的窗格中。 run-shell 也不这样做。
      • 不确定我是否理解您的问题,但bind-key X send-key "ssh HOST\n" 是否按照您的意愿行事?
      • 有点像... :)thanx。顺便说一句:它没有执行 "\n" .... 我发现它是 C-m 而不是 \n
      【解决方案4】:

      我正在使用 tmux 1.8 并没有找到内置的解决方案。这些解决方法至少适合我的常见用例:

      • 捕获整个窗格内容并在其中搜索最后一个 ssh 命令(我使用提示结束的知识来或多或少可靠地检测命令)
      • 如果失败,我会使用 tmux new-windowsplit-window 命令的 shell-command 选项检查创建窗格的命令

      我的reconnect.sh 脚本如下所示。它最脏的地方是从缓冲区中获取最后一个 ssh 命令的方式。到目前为止,“> ssh”足以让我的情况可靠地检测到包含 ssh 连接请求的线路,但如果有更好的解决方案,我们将不胜感激。

      #!/bin/bash
      
      # @TODO: change this according to your own prompt
      # This is used to find lines connect ssh command in the pane buffer
      PROMPT_SEPARATOR="> "
      
      # get current pane buffer size and dimensions
      HISTORY_LIMIT=`tmux display-message -p "#{history_limit}"`
      VISIBLE_LINES=`tmux display-message -p "#{pane_height}"`
      
      # search last ssh command in pane content
      LINE=`tmux capture-pane -p -J -S -$HISTORY_LIMIT -E $VISIBLE_LINES | grep "${PROMPT_SEPARATOR}ssh " | tail -1`
      if [ -n "$LINE" ]; then
          echo $LINE | sed "s/.*$PROMPT_SEPARATOR//;"
      else
          # fall back to the command that might have been used to create the pane
          # (not necessarily ssh but helpful anyway)
          tmux list-panes -F "#{pane_active} #{pane_start_command}" | grep "^1 " | tail -1 | cut -d ' ' -f2-
      fi
      

      我将此脚本保存在我的 ~/.tmux 目录中,并更改了我的 .tmux.conf 中各种 split-windownew-window 快捷方式的键绑定,类似于:

      # try to reconnect to remote host when creating new window
      bind c run-shell 'CMD=`~/.tmux/reconnect.sh`; tmux new-window "$CMD"'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-31
        • 2012-02-29
        • 1970-01-01
        • 2020-09-25
        • 1970-01-01
        • 2011-11-17
        • 2023-03-16
        • 1970-01-01
        相关资源
        最近更新 更多