Small shell script to start multiple connections to SSH servers with synchronized keyboard.
If you need to connect to multiple SSH machines and perform some operation on all of them, there are many ways to do this. Typically regular sysadmin would write simple
for one-liner in shell:
for i in $HOSTS; do <....> done
It's OK for simple command or couple of commands. But, if commands produce verbose output, or you want to see what's happening in between your commands, this approach can be more troublesome. Or, for example, your ssh keys are not set and you'd need to type password for each SSH prompt.
tmux you can do it in a different way. If you haven't started with
tmux yet, there's a good reason to do it.
This script allows with single command open multiple equally split-panes in Tmux window and synchronizes keyboard input in all windows. Anything you type will go at the same time to all
tmux panes and you will see output of commands in all windows as they are executed. Any time it is you can break keyboard sync between panes if you need to execute something in single shell only, then you can sync again and continue as before.
Full text of the script is provided in the Gist below. It is based on solution I've found online, a bit simplified and extended to accept list of hosts from command line.
I usually run it as:
ssh-multi myhost1 myhost2 myhost3 myhost4
or for some groups of hosts, I put list into plain text file:
ssh-multi $(cat list-of-hosts)
The script is included in dotfiles repository. You can eiterh copy-paste from included Gist, or simply get the whole thing, by cloning o downloading it using links below.
Blog post regarding Tmux environment configuration
Script source code
Dmytro Kovalov (email@example.com)