The new-session
and split-pane
commands in tmux
take a command to run in the new pane. If you have a list of user@server
strings in an array, you could do this:
#!/bin/bash
ssh_list=( user1@server1 user2@server2 ... )
split_list=()
for ssh_entry in "${ssh_list[@]:1}"; do
split_list+=( split-pane ssh "$ssh_entry" ';' )
done
tmux new-session ssh "${ssh_list[0]}" ';' \
"${split_list[@]}" \
select-layout tiled ';' \
set-option -w synchronize-panes
This creates a list of split-pane
commands that split the current pane and run ssh
to one of the servers in the list ssh_list
. We create one such command for each of the listed servers, except for the first one (which we will use with the new-session
command instead).
The tmux
command at the end creates a new session and runs the first ssh
command, does all the splits, rearranges the panes according to the tiled
layout (see the tmux
manual for alternatives), and enables synchronisation of the panes.
The tmux
session will terminate once all ssh
sessions have terminated.
Update: Interestingly, the tmux(1)
manual does not mention a split-pane
command. The above code still works though, but if you're using an older version of tmux
, you may want to change split-pane
into split-window
(since tmux
2.4, split-pane
and splitp
are synonyms for split-window
).
tmux split-window
will create a new pane, andtmux send-keys
can send commands to start an ssh session.tmux new-window ssh server01.local \; split-pane -h ssh server02.local
to create a new window with 2 splits with different ssh session each. maybe this can be extended.