Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Start multiple synchronized SSH connections with Tmux
#!/bin/bash
# ssh-multi
# D.Kovalov
# Based on http://linuxpixies.blogspot.jp/2011/06/tmux-copy-mode-and-how-to-control.html
# a script to ssh multiple servers over multiple tmux panes
starttmux() {
if [ -z "$HOSTS" ]; then
echo -n "Please provide of list of hosts separated by spaces [ENTER]: "
read HOSTS
fi
local hosts=( $HOSTS )
tmux new-window "ssh ${hosts[0]}"
unset hosts[0];
for i in "${hosts[@]}"; do
tmux split-window -h "ssh $i"
tmux select-layout tiled > /dev/null
done
tmux select-pane -t 0
tmux set-window-option synchronize-panes on > /dev/null
}
HOSTS=${HOSTS:=$*}
starttmux
@cpanato

This comment has been minimized.

Copy link

@cpanato cpanato commented Jan 21, 2014

cool!

@nodirt

This comment has been minimized.

Copy link

@nodirt nodirt commented May 13, 2014

Awesome, thanks

@belminf

This comment has been minimized.

Copy link

@belminf belminf commented Aug 25, 2015

Great little function. Thanks!

BTW, if you have servers that are sequentially indexed, you could leverage brace expansion like so:

$ startmux foo{1..9}

Or, for a select few:

$ startmux foo{0,5}
@Gibby

This comment has been minimized.

Copy link

@Gibby Gibby commented Jan 8, 2016

In tmux 2.1 this no longer works correctly, the first host opens in a new-window but the rest open in the current window. I went back to 2.0 and it works correctly. Haven't been able to figure out the issue yet.

@pwojt

This comment has been minimized.

Copy link

@pwojt pwojt commented Jan 22, 2016

Little mod for Tmux 2.1 need to add a target for each event. This sets the target to ssh-multi with the first host in the name:

#!/bin/bash
# ssh-multi
# D.Kovalov
# Based on http://linuxpixies.blogspot.jp/2011/06/tmux-copy-mode-and-how-to-control.html

# a script to ssh multiple servers over multiple tmux panes


starttmux() {
    if [ -z "$HOSTS" ]; then
       echo -n "Please provide of list of hosts separated by spaces [ENTER]: "
       read HOSTS
    fi

    local hosts=( $HOSTS )
    local target="ssh-multi ${host[0]}"


    tmux new-window -n "${target}" ssh ${hosts[0]}
    unset hosts[0];
    for i in "${hosts[@]}"; do
        tmux split-window -t :"${target}" -h  "ssh $i"
        tmux select-layout -t :"${target}" tiled > /dev/null
    done
    tmux select-pane -t 0
    tmux set-window-option -t :"${target}"  synchronize-panes on > /dev/null

}

HOSTS=${HOSTS:=$*}

starttmux

edit: small change to make target correct.

@nomad-fr

This comment has been minimized.

Copy link

@nomad-fr nomad-fr commented Mar 9, 2016

Hi I did a fork here : https://gist.github.com/nomad-fr/8f2316c24c9d488a603b
it check that you are inside a tmux session and if not create one
I add some options like :
-u to change user for ssh set by defaut to root
-d (destination) then you can specified several host
-d "$(echo 'serv'{0..3})"
-d "serv0 serv1 serv2 serv3"'
-d "$(anotherscript)" # to call a script that give you a list of host

@joefromct

This comment has been minimized.

Copy link

@joefromct joefromct commented Jun 21, 2016

@nomad-fr https://gist.github.com/nomad-fr/8f2316c24c9d488a603b page not found?

thanks nice little script.

@joefromct

This comment has been minimized.

@olsonbg

This comment has been minimized.

Copy link

@olsonbg olsonbg commented Jul 3, 2016

Great, thanks. I've included some more additions based on the work of @nomad-fr, including:

  • Option to easily have localhost in the synchronized panes (-l),
  • No longer need to specify a user name (so that ~/.ssh/config settings will be used), and
  • No longer opens an extra tmux window when a new session is used.

https://github.com/olsonbg/dotfiles/blob/master/bin/ssh-multi.sh

@greymd

This comment has been minimized.

Copy link

@greymd greymd commented May 12, 2017

Not fork of it, but
FYI: https://github.com/greymd/tmux-xpanes

$ xpanes -c "ssh {}" serv1 serv2 serv3 ...
@biocyberman

This comment has been minimized.

Copy link

@biocyberman biocyberman commented Aug 22, 2017

@greymd xpanes is much more then a mere multi-ssh script. Thanks for sharing it.

@sillykelvin

This comment has been minimized.

Copy link

@sillykelvin sillykelvin commented May 24, 2018

Great little script! Especially this one: tmux set-window-option synchronize-panes on > /dev/null, it's something magic for me! I was using clusterssh before for the same feature, and never know tmux can do this!

BTW: the script in original post works well with tmux 2.1 for me, on a Ubuntu 16.04 box with tmux installed by apt, tmux -V says tmux 2.1.

@ttroutman

This comment has been minimized.

Copy link

@ttroutman ttroutman commented Aug 11, 2018

@greymd xpanes! - Thank you! I've been looking for exactly this off and on for two years.

@WMcKibbin

This comment has been minimized.

Copy link

@WMcKibbin WMcKibbin commented Mar 29, 2019

#!/bin/bash
#tmux-ssh.sh                                                                                                                                                                                                         
# Assume session is 0
tmux attach-session -t 0
WINDOW=$(tmux display-message -p "#I")
FIRST_ARG=""

for arg in $*; do
    if [[ $arg != *"%"* ]]; then
        if [[ $FIRST_ARG == "" ]]; then
            FIRST_ARG=$arg
            tmux send-keys "ssh $arg" Enter
        else
            tmux split-window "ssh $arg"
        fi
        tmux select-layout tiled
    fi
done
tmux set-window-option synchronize-panes on

This is how my old co-worker accomplished this, figured I'd drop in this thread not as a replacement but an alternate option.

@abunimeh

This comment has been minimized.

Copy link

@abunimeh abunimeh commented Sep 11, 2020

#!/usr/bin/env bash
instead of of hardcoded shebang

@Kimeg

This comment has been minimized.

Copy link

@Kimeg Kimeg commented Jan 11, 2021

Thank you, sir. I found your code very useful for setting environments across multiple servers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment