liuliancao
2021-05-12 08:44:18 +08:00
#!/usr/bin/env bash
# usage: ssh-multi for tmux by liuliancao at 2021/04/22 v1.
# a script to ssh multiple servers over multiple tmux panes
while getopts p:u:f: OPTION
do
case $OPTION in
p)PORT=$OPTARG;;
u)USER=$OPTARG;;
f)FILE=$OPTARG;;
?)echo "use ssh-multi -p $PORT -u $USER -f ssh-hosts-file" && exit 1;;
esac
done
index=-1
# split window to ssh
cat $FILE | while read host; do
index=$(($index + 1))
#if in tmux
if [[ -z $TMUX ]];then
in_tmux=0
[[ $index -eq 0 ]] && tmux new-session -d "ssh -p $PORT $USER@$host" && continue
tmux split-window -h "ssh -p $PORT $USER@$host"
# if not
else
in_tmux=1
[[ $index -eq 0 ]] && tmux new-window -n "ssh-multi" "ssh -p $PORT $USER@$host" && continue
tmux split-window -t "ssh-multi" "ssh -p $PORT $USER@$host"
fi
tmux select-layout tiled
done
tmux set-window-option synchronize-panes on
[[ in_tmux -eq 0 ]] && tmux a