How to duplicate ssh session on tmux

I want to duplicate the ssh session again.

For example, my windowname is "user @host". I want to press prefix key + S to do "ssh user @host in new window"

$ tmux bind S confirm-before "neww ssh #W"

      

After you try it, it just issues ssh command without 'user @host' option. Tmux version is 1.8 on CentOS 7.

+3


source to share


1 answer


You can try something like this, although it's a little ugly. Put this in your tmux.conf

:

bind S neww "$(ps -ao pid,tty,args | sort | awk '$1 ~ /#{pane_pid}/{VAR=$2} $2 ~ VAR && $3 ~ /ssh/{$1=\"\"; $2=\"\"; print}')"

      

Explanation

Create a named binding S

and open it in a new window using the argument as an initial command

bind S neww "..."

      

Execute internal command output

$(...)

      

List the pid, tty and command commands (with arguments) of all processes

ps -ao pid,tty,args | ...

      

Sort by pid



... | sort | ...

      

Submitting to awk

... | awk '...'

      

Find the tty of the current area / window and put it in VAR

( #{}

replaced with tmux)

$1 ~ /#{pane_pid}/{VAR=$2}

      

Find the process that has the tty we found earlier. And has a command starting with ssh

. Note that we assume that the pid of the ssh session is larger than the shell it was called into. This should be true in most cases.

$2 ~ VAR && $3 ~ /ssh/{...}

      

Remove pid, tty and print the remainder. This will be an ssh command with all arguments and options. This is the command that will be executed in a new window.

$1=\"\"; $2=\"\"; print

      

+3


source







All Articles