Last active
May 4, 2019 11:00
-
-
Save Ahmadposten/e9a96855d974448094fef858141cb93b to your computer and use it in GitHub Desktop.
Use tmux to ssh to all instances of an AWS autoscaling group and execute commands simultaneously.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# _ _ _ | |
# ____ | | | | | | | |
# / __ \ __ _| |__ _ __ ___ __ _ __| |_ __ ___ ___| |_ ___ _ __ | |
# / / _` |/ _` | '_ \| '_ ` _ \ / _` |/ _` | '_ \ / _ \/ __| __/ _ \ '_ \ | |
# | | (_| | (_| | | | | | | | | | (_| | (_| | |_) | (_) \__ \ || __/ | | | | |
# \ \__,_|\__,_|_| |_|_| |_| |_|\__,_|\__,_| .__/ \___/|___/\__\___|_| |_| | |
# \____/ | | | |
# |_| | |
##################################################### | |
# | |
# Use this script to ssh to multiple instances in an AWS autoscaling group using tmux. It synchronizes all the panes | |
# so you can execute the same command on all of the machines simultaneously. Dependences are tmux and aws cli. AWS cli | |
# needs to be configured | |
# configurations. | |
# You have to set these before running the script | |
AUTO_SCALING_GROUP_NAME="my group name" | |
SSH_KEY_PATH="my key path" | |
USER="my username (the ssh user)" | |
if ! command -v "aws" >/dev/null 2>&1; then | |
echo "AWS cli is not installed. please follow 'http://docs.aws.amazon.com/cli/latest/userguide/installing.html'" | |
fi; | |
if ! command -v "tmux" >/dev/null 2>&1; then | |
echo "Please install tmux"; | |
fi; | |
HOSTS=( $(aws --output text --query "Reservations[*].Instances[*].PublicIpAddress" ec2 describe-instances --instance-ids\ | |
`aws --output text --query "AutoScalingGroups[0].Instances[*].InstanceId" autoscaling describe-auto-scaling-groups \ | |
--auto-scaling-group-names "$AUTO_SCALING_GROUP_NAME"` | tr "\n" " " | tr "\t" " ")) | |
tmux new-window "ssh $USER@${HOSTS[0]} -i $SSH_KEY_PATH" | |
HOSTS=("${HOSTS[@]:1}") | |
for i in "${HOSTS[@]}"; do | |
tmux split-window -h "ssh $USER@$i -i $SSH_KEY_PATH" | |
tmux select-layout tiled > /dev/null | |
done | |
tmux select-pane -t 0 | |
tmux set-window-option synchronize-panes on > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DEMO