view · edit · history · print

tmux

Here is a quick script that can be improved. Not sure if the sleep functions are required, not sure if there are some extra selections, it can be a bit more dynamic and function oriented but for a first try it does a nice job!

PS: The Linux 'terminator' (application for multiple GNOME terminals in one window) can also broadcast typed input out-of-the-box.

[wvh@sapocvm1 ~]$ ./terms v sapocvm3 sapocvm2 sapocvm1

#!/bin/bash

# CONSTANT - never changes (unless the script is adjusted)
terms=8 # number of terminals to create - considered CONSTANT!
session_name=sshsync

# get special overwrite parameters
forcelayout=0 # tiled layout
if [ "${1}" == "t" -o "${1}" == "v" -o "${1}" == "h" ]
then
  forcelayout=$1
  shift
fi


# set some variables
args=$# # number of arguments representing remote sessions
exits=$(expr ${terms} - ${args} ) # number of terms too many we will generate
reallignterms=5 # when to reallign to all horizontal


# verify arguments
if [ ${args} -lt 1 ]
then
  echo "ERR: please provide 1 to 8 servernames as arguments"
  echo "
        USAGE: $0 [t|v|h] <SpaceSepparatedHosListt[1-8]>
          t = tiled layout, spread out as evenly in both rows and columns.
          v = vertical tiled layout (wide output panes).
          h = horizontally tiled layout (narrow long panes).

        EXAMPLE: $0 127.0.0.1 localhost
  "
  exit 1
fi

if [ ${args} -gt 8 ]
then
  echo "ERR: please provide maximum 8 servernames as arguments"
  exit 1
fi


#this is going to setup the session and detach
tmux new-session -s ${session_name} -d
sleep 1


#this is going to split the pane into four even horizontal panes
tmux split-window -t ${session_name}:0.0 -v -d
tmux split-window -t ${session_name}:0.0 -v -d
tmux split-window -t ${session_name}:0.0 -v -d
tmux split-window -t ${session_name}:0.0 -v -d
sleep 1


# closing extra terminal and even out vertical spacing
tmux send-keys C-c
tmux send-keys "exit" C-m
sleep 1
tmux select-layout -t ${session_name}:0 even-vertical
sleep 1


#this will split each of the four panes vertically, resulting in eight panes
for i in 0 2 4 6
do
  tmux select-pane -t ${session_name}:0.$i
  tmux split-window -t ${session_name}:0.$i -h
  sleep 1
done


#this goes to each pane and sends a command
tmux select-pane -t ${session_name}:0.0
tmux send-keys -t ${session_name}:0.0 "ssh $(logname)@$1" C-m
tmux select-pane -t ${session_name}:0.1
tmux send-keys -t ${session_name}:0.1 "ssh $(logname)@$2" C-m
tmux select-pane -t ${session_name}:0.2
tmux send-keys -t ${session_name}:0.2 "ssh $(logname)@$3" C-m
tmux select-pane -t ${session_name}:0.3
tmux send-keys -t ${session_name}:0.3 "ssh $(logname)@$4" C-m
tmux select-pane -t ${session_name}:0.4
tmux send-keys -t ${session_name}:0.4 "ssh $(logname)@$5" C-m
tmux select-pane -t ${session_name}:0.5
tmux send-keys -t ${session_name}:0.5 "ssh $(logname)@$6" C-m
tmux select-pane -t ${session_name}:0.6
tmux send-keys -t ${session_name}:0.6 "ssh $(logname)@$7" C-m
tmux select-pane -t ${session_name}:0.7
tmux send-keys -t ${session_name}:0.7 "ssh $(logname)@$8" C-m
#tmux select-pane -t ${session_name}:0.8


# closing some panes that did not receive any useful ssh target
echo "${exits} exits"
while [ ${exits} -gt 0 ]
do
  tmux send-keys C-c
  tmux send-keys "exit" C-m
  echo "send ${exits}"
  exits=$(expr ${exits} - 1 )
done
sleep 1


# when below a certain ammount of panes we re-allign vertical spacing
case $forcelayout in
"t")
  tmux select-layout -t ${session_name}:0 tiled
  ;;
"v")
  tmux select-layout -t ${session_name}:0 even-vertical
  ;;
"h")
  tmux select-layout -t ${session_name}:0 even-horizontal
  ;;
*)
  if [ ${args} -lt ${reallignterms} ]
  then
    tmux select-layout -t ${session_name}:0 even-vertical
  fi
  ;;
esac


# do the magic sync
tmux set-window-option -t ${session_name}:0 synchronize-panes on


# attach and list after tmux exit
tmux attach-session -t ${session_name}
tmux ls

References

admin · attr · attach · edit · history · print
Page last modified on November 14, 2014, at 09:40 AM