Skip to content

Instantly share code, notes, and snippets.

@particledecay
Created August 28, 2019 20:48
Show Gist options
  • Save particledecay/034bef3458486fcc65174155c5f9cec2 to your computer and use it in GitHub Desktop.
Save particledecay/034bef3458486fcc65174155c5f9cec2 to your computer and use it in GitHub Desktop.
SSH to all instances within an AWS target group
#!/bin/bash
ssh_user=joey.espinosa
aws_region=us-west-1
export target_group=$1
# grab AWS info
target_arn=$(aws elbv2 describe-target-groups --region ${aws_region} --query "TargetGroups[?TargetGroupName==\`${target_group}\`].TargetGroupArn" --output text)
instance_ids=$(aws elbv2 describe-target-health --target-group-arn $target_arn --region ${aws_region} --query "TargetHealthDescriptions[*].Target.Id" --output text)
ip_addresses=( $(aws ec2 describe-instances --region ${aws_region} --instance-ids $instance_ids --query "Reservations[*].Instances[*].PrivateIpAddress" --output text ) )
# start a new tmux session in the background
tmux_name="tgt_$(date +%Y%m%d%H%M)"
tmux new -d -s $tmux_name "ssh ${ssh_user}@${ip_addresses[0]}"
# split a new pane for every IP address
for (( i=1; i<${#ip_addresses[@]}; i++ )); do
tmux splitw -h "ssh ${ssh_user}@${ip_addresses[i]}"
tmux select-layout tiled # we do this each time so we can make room for the next pane
done
# attach to your new tmux session
tmux a -t $tmux_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment