Last active
March 18, 2016 12:31
-
-
Save rueian/cde1cf0191a97caab8cc to your computer and use it in GitHub Desktop.
Start multiple synchronized SSH connections with Tmux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ssh-multi : a script to ssh multiple servers over multiple tmux panes | |
# nomad-fr : https://gist.github.com/nomad-fr/8f2316c24c9d488a603b | |
# Based on D.Kovalov work : https://gist.github.com/dmytro/3984680 | |
# config | |
#user=$USER # user use for ssh connection | |
user=root | |
usage() { | |
echo $1 | |
echo | |
echo 'ssh-multi.sh : [OPTION]' | |
echo ' -u user : user use for ssh connection' | |
echo ' -d "serv0 serv1 serv2 ... servN" : list serv to connect to' | |
echo | |
echo ' Bonus:' | |
echo ' -d "$(echo 'serv'{0..3})" : is the same as : -d "serv0 serv1 serv2 serv3"' | |
echo ' -d "$(anotherscript)" : call a script that give a list of host separated by space' | |
exit 0 | |
} | |
starttmux() { | |
local hosts=( $HOSTS ) | |
local target="ssh-multi ${host[0]}" | |
tmux new-window -n "${target}" ssh $user@${hosts[0]} | |
unset hosts[0]; | |
for i in "${hosts[@]}" | |
do | |
tmux split-window -t :"${target}" -h "ssh $user@$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 | |
} | |
checkopt() { | |
if [ -z "$NAME" ]; then | |
usage "Please provide the session name." | |
fi | |
if [ -z "$HOSTS" ]; then | |
usage "Please provide of list of hosts with -d option." | |
fi | |
if [ -z "$TMUX" ]; then # if not in a tmux session create one | |
tmux -u new-session -d -s "$NAME" | |
local launchtmux=1 | |
fi | |
starttmux | |
if [ "$launchtmux" = 1 ]; then tmux a -dt "$NAME"; fi | |
} | |
while getopts "n:u:d:h" o; do | |
case "${o}" in | |
h) | |
usage | |
;; | |
u) | |
user=${OPTARG} | |
;; | |
d) | |
HOSTS=${OPTARG} | |
;; | |
n) | |
NAME=${OPTARG} | |
;; | |
esac | |
done | |
checkopt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment