Q:

How to duplicate ssh session on tmux

I want to duplicate my ssh session again.
For example, my window-name is "user@host'. I wish to press prefix key + S to do 'ssh user@host on a new window'
$ tmux bind S confirm-before "neww ssh #W"

After try this, it just issue a ssh command without the option 'user@host'
The tmux version is 1.8 on CentOS 7.

A:

You can try something like this, though it is a little ugly. Place this into 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
Creat a binding named S and have it open a new window, using the argument as the initial command
bind S neww "..."

Execute the output of the inner command
$(...)

List the pid, tty, and command (with arguments) of all processes
ps -ao pid,tty,args | ...

Sort by pid
... | sort | ...

Feed output into awk
... | awk '...'

Find tty of current pane/window, and place it in VAR (#{} is substituted by tmux)
$1 ~ /#{pane_pid}/{VAR=$2}

Find process that has the tty we found earlier AND has a command that starts with ssh. Note that we are assuming that the pid of the ssh session is greater than the shell it was invoked in. This should be true in most cases.
$2 ~ VAR && $3 ~ /ssh/{...}

Remove pid, tty, and print the remainder. This will be the ssh command with all arguments and options. This is the command that will get executed in a new window.
$1=\"\"; $2=\"\"; print

