Skip to content

Instantly share code, notes, and snippets.

@jamesandariese
Last active September 9, 2015 18:05
Show Gist options
  • Save jamesandariese/a03dcee33c821acf91f2 to your computer and use it in GitHub Desktop.
Save jamesandariese/a03dcee33c821acf91f2 to your computer and use it in GitHub Desktop.
tmux-create-window-for-many-hosts-tj-edition
#!/bin/sh
# A split window generator for tmux.
# Copyright 2014, James Andariese
# For what it's worth.
# MIT license.
# If things go wrong, it tends to leave either a single or many windows open with your local shell.
# I don't fix that because I don't care even a little bit.
# Don't do things wrong and be prepared to hit control D if you do. The cost of fixing is greater.
# You will also need the following (or similar) in your tmux.conf
# bind * command-prompt -p "hosts" "run '~/bin/tmux-create-windows-for-many-hosts-tj-edition %%'"
# bind ^ setw sync
# If you do C-b *, you will be presented with a prompt. If you enter api{1..3} web{3..6}, it will
# attempt to connect, as root, to apis 1 through 3 and webs 3 through 6.
# If you enter instead ubuntu@api{1..3} root@web{3..6}, it will connect to the same hosts but
# the apis will be connected to as the user "ubuntu".
# Pro tip: don't name your hosts with an @ in -- it's impossible.
truth() {
# This version allows bash generators on the command line
bash -c "echo $1"
# This version gets truth from the ssh config
#cat ~/.ssh/config | awk '/^ *Host / {print $2}'
}
justice() {
# This version allows bash generators on the command line
cat
# This version gets truth from the ssh config
#grep -E "$1"
}
connect() {
SPLIT=no #don't split first time
for X in `truth "$1" | justice "$1"`;do
if [ $SPLIT = yes ];then
#don't split first time
tmux split-window
tmux select-layout tiled > /dev/null
fi
SPLIT=yes
tmux send 'ssh -l '"$SSHUSER"' "'"$X"'" ; exit' 'enter'
done
}
tmux neww
while [ "$#" -gt 0 ];do
connect "$1"
shift
[ $# -lt 1 ] && break
tmux split-window
tmux select-layout tiled > /dev/null
done
tmux setw sync > /dev/null 2>&1 # sync by default or not?
@jamesandariese
Copy link
Author

Truth for finding all the hosts. Justice for culling the herd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment