sten
sten

Reputation: 7476

Can tmux pane remote ssh-connection

Can I run tmux locally and connect via ssh to remote machine.. and after that any new pane and/or screen to be with the remote-machine-shell... What I'm saying I can't install tmux on the remote machine, but I don't want to do a ssh connection from every pane, but ssh-login just once.

Is such thing possible.. thanks

Upvotes: 11

Views: 11146

Answers (4)

Thorsten
Thorsten

Reputation: 15

I am using tmux 1.8 and did not find a built-in solution. These workarounds fit at least for my common use cases:

  • Capture the full pane content and search for the last ssh command in it (I use the knowledge about the ending of my prompt to detect the command more or less reliably)
  • If this fails I check the command the pane might have been created with by using the shell-command option of tmux new-window or split-window commands

My reconnect.sh script looks like this. The most dirty thing about it is the way to get the last ssh command from the buffer. Up to now "> ssh " was enough for my situations to reliably detect a line containing a ssh connection request but any better solution would be appreciated.

#!/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

I saved this script in my ~/.tmux directory and changed key bindings for various split-window and new-window shortcuts in my .tmux.conf similar to this:

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

Upvotes: 0

chepner
chepner

Reputation: 531165

lilydjwg explained something I never really understood before. Knowing about the ControlMaster setting makes the following much more reasonable, as it simplifies making multiple ssh connections. You only need to authenticate once, and the remote host doesn't need to have an sshd process running for each connection.

In your .tmux.conf file:

# 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 ""

Upvotes: 5

lilydjwg
lilydjwg

Reputation: 1713

If you want to login just once, you can use ControlMaster feature of ssh. Add some config like this to your ~/.ssh/config:

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

If you login to the same server (as the same user) multiple times (either in one tmux or not), ssh will reuse the connection so that you don't need to make connection and login again.

Upvotes: 7

Thor
Thor

Reputation: 47099

I don't think tmux can. One workaround would be to add something like this to tmux.conf.

bind-key X new-window "ssh HOST"

Then new windows would start at the remote host.

Upvotes: 3

Related Questions