Skip to content

Instantly share code, notes, and snippets.

@chris2k20
Forked from pstray/ssh-multi.sh
Created May 1, 2020 12:04
Show Gist options
  • Save chris2k20/418ffb894d38c2aa0ee6f6b8c8ccace8 to your computer and use it in GitHub Desktop.
Save chris2k20/418ffb894d38c2aa0ee6f6b8c8ccace8 to your computer and use it in GitHub Desktop.
Start tmux and ssh to multiple hosts with synchronized input
#! /bin/bash
if [ -z "$1" ]; then
echo "please supply at least one server to connect to" >&2
exit 1
fi
target=multi-ssh-$$
if [ -z "$TMUX" ]; then
tmux new-session -d -s "$target"
fi
if [ -n "$(type -p autossh 2> /dev/null)" ]; then
ssh="AUTOSSH_GATETIME=${AUTOSSH_GATETIME:-0} autossh"
else
ssh="ssh"
fi
tmux new-window -n "$target" "$ssh $1"
shift
for host in "$@"; do
tmux split-window -t ":$target" -h "$ssh $host"
tmux select-layout -t ":$target" tiled #> /dev/null
done
tmux select-pane -t ":$target"
tmux set-window-option -t ":$target" synchronize-panes on #>/dev/null
if [ -z "$TMUX" ]; then
tmux attach-session -d -t ":$target"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment