52 lines
1.2 KiB
Bash
Executable File
52 lines
1.2 KiB
Bash
Executable File
#!/bin/bash -x
|
|
|
|
#set -o errexit
|
|
set -o nounset
|
|
set -o pipefail
|
|
|
|
SESSION=$AWS_PROFILE
|
|
EC2_INSTANCES=$(aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" --query "Reservations[*].Instances[*].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" --output text|sed -E 's/\s+/,/g')
|
|
|
|
get_window() {
|
|
NAME=$1
|
|
window_id=$(tmux list-windows -t $SESSION -F "#W:#I" |grep $NAME|cut -d':' -f2)
|
|
echo "$window_id"
|
|
}
|
|
|
|
create_window() {
|
|
NAME=$1
|
|
COUNT=$(tmux list-windows -t $SESSION|wc -l)
|
|
WINDOW_ID=$(($COUNT))
|
|
tmux new-window -t $SESSION:$WINDOW_ID -n $NAME
|
|
}
|
|
|
|
attach_to_window() {
|
|
NAME=$1
|
|
HOST=$2
|
|
WINDOW=$(get_window $NAME)
|
|
if [ "$WINDOW" = "" ]
|
|
then
|
|
create_window $NAME
|
|
tmux send-keys -t $NAME "jssh $HOST" C-m
|
|
else
|
|
tmux split-window -t $NAME
|
|
tmux send-keys -t $NAME "jssh $HOST" C-m
|
|
fi
|
|
}
|
|
|
|
|
|
tmux has-session -t $SESSION 2>/dev/null
|
|
if [ $? != 0 ]; then
|
|
tmux new-session -d -s $SESSION
|
|
for INSTANCE in $EC2_INSTANCES
|
|
do
|
|
HOST=$(echo $INSTANCE|cut -d',' -f1)
|
|
NAME=$(echo $INSTANCE|cut -d',' -f2)
|
|
attach_to_window $NAME $HOST
|
|
done
|
|
fi
|
|
|
|
tmux attach-session -t $SESSION
|
|
|
|
|