Skip to content

Instantly share code, notes, and snippets.

@dialtone
Created October 31, 2013 03:42
Show Gist options
  • Save dialtone/7244094 to your computer and use it in GitHub Desktop.
Save dialtone/7244094 to your computer and use it in GitHub Desktop.
Given a few parameters connect to a set of ec2 instances, one per tmux pane
#!/bin/bash
SSH_KEY=
USERNAME=
usage() { echo "Usage: $0 -s <tmux_session_name> [-k <my_ssh_key.pem>] [-u <username>] <host> <host1> ..." 1>&2; exit 1; }
while getopts "s:k:u:" option;
do
case "${option}" in
s) SESSION_NAME=${OPTARG};;
k) SSH_KEY=${OPTARG};;
u) USERNAME=${OPTARG};;
*) usage;;
esac
done
if [[ "$SESSION_NAME" == "" ]]; then
usage
fi
if [[ "$SSH_KEY" == "" ]]; then
SSH_KEY_PART=
else
SSH_KEY_PART="-i $SSH_KEY"
fi
if [[ "$USERNAME" == "" ]]; then
USERNAME_PART=
else
USERNAME_PART="$USERNAME@"
fi
shift $((OPTIND-1))
HOSTS=($@)
tmux new -d -s $SESSION_NAME
for index in ${!HOSTS[*]}
do
if [[ "$index" == "0" ]]; then
continue
fi
tmux split-window -t $SESSION_NAME.0 -h
done
tmux select-layout -t $SESSION_NAME even-horizontal
for index in ${!HOSTS[*]}
do
host=${HOSTS[$index]}
tmux send -l -t $SESSION_NAME.$index "ssh $SSH_KEY_PART $USERNAME_PART$host"
done
tmux attach -t $SESSION_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment