Skip to content

Instantly share code, notes, and snippets.

@henri
Forked from dmytro/ssh-multi.sh
Last active January 27, 2025 01:19
Show Gist options
  • Save henri/263441168daf8cffec2ba5d56a496711 to your computer and use it in GitHub Desktop.
Save henri/263441168daf8cffec2ba5d56a496711 to your computer and use it in GitHub Desktop.
Start multiple synchronized SSH connections with Tmux
#!/bin/bash
# ssh-multi
# Modified to check if the script is started within a TMUX session to avoid starting in a background TMUX session
# H.SHUSTAK
# Origional :
# D.Kovalov : https://gist.github.com/dmytro/3984680
# 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
checktmux() {
if [ -z "${TMUX}" ] ; then
echo "NOTE : This script is not being started within a tmux session"
echo " It is reccomended to start this script from within a tmux session."
exit -9
fi
}
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:=$*}
checktmux
starttmux
@henri
Copy link
Author

henri commented Jan 24, 2025

nice : +1:

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