2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tmuxで同時にコマンド実行

Last updated at Posted at 2013-07-11

window内の行数、列数指定
超えると次のwindow作成
window内はコマンド同期
/etc/hostsからサーバー取得

# !/bin/bash

HOSTS_FILE="/etc/hosts"

row=$1
column=$2
hostname_prefix=$3

hostname=(`grep ${hostname_prefix} ${HOSTS_FILE} | awk '{ print $2 }' | grep ^${hostname_prefix}`)

tmux start-server
tmux new-session -d -s tmux-ssh

window_num=0

for ((i=0;i<${#hostname[@]};i++)); do
  if [ `expr ${i} % \( ${column} \* ${row} \)` -eq 0 ]; then
    window_num=`expr ${i} / \( ${column} \* ${row} \)`
    window_name="tmux-ssh-window${window_num}"
    tmux new-window -n ${window_name}
    tmux set-window-option -t ${window_name} synchronize-panes on
    tmux select-window -t ${window_name}
    tmux split-window -h -t ${window_name}
    tmux select-layout -t ${window_name} main-horizontal
    pane0=${i}
  elif [ ${i} -lt `expr ${pane0} + ${column}` ]; then
    tmux split-window -h -t ${window_name}
    tmux select-layout -t ${window_name} main-horizontal
  else
    row_num=`expr \( ${i} / ${column} \) % ${row} + 1`
    column_num=`expr \( ${i} % ${column} \) + 1`
    tmux select-pane -t `expr ${row_num} \* ${column_num} - 1`
    tmux split-window -v -t ${window_name}
  fi
  tmux send-keys "ssh ${hostname[i]}" C-m
done

for i in `seq 0 ${window_num}`;do
  tmux select-window -t tmux-ssh-window${i}
  tmux kill-pane -t 0
done

tmux select-window -t tmux-ssh-window0
tmux kill-window -t 0
tmux attach-session -t tmux-ssh

例)/etc/hostsからdev-wap*のホスト名を取得し一斉ログイン。1window内は2行3列

./tmux_ssh 2 3 dev-wap
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?