Skip to content

Instantly share code, notes, and snippets.

@ecaron
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save ecaron/9b6ad8e8dd97e01f00bc to your computer and use it in GitHub Desktop.

Select an option

Save ecaron/9b6ad8e8dd97e01f00bc to your computer and use it in GitHub Desktop.
Dynamic shell script to use cssh to connect to multiple EC2 boxes by tag-name, via AWS CLI
#!/bin/bash
# Requirements:
# * AWS CLI installed and activated
# * cssh installed
#
# Usage:
# * Run "cssh-ec2-connector.sh some-tag-name" to have system open windows to all instances
# * Edit the ec2Tags array to have a list of frequently used instances for repeat use
# This is your SSH key for your AWS boxes
AWS_SSH_KEY="./ssh/aws-key.pem"
declare -A ec2Tags
# The key will be how you refer to the instance, the value will be the literal tag known to EC2
# Remember, this is bash, so no spaces around the equal sign
ec2Tags["Tag Set A"]="set-a"
ec2Tags["Another Tag Set"]="beta-group"
## DO NOT MODIFY BENEATH THIS POINT
## DO NOT MODIFY BENEATH THIS POINT
## DO NOT MODIFY BENEATH THIS POINT
## DO NOT MODIFY BENEATH THIS POINT
## DO NOT MODIFY BENEATH THIS POINT
cssh_ec2_connect(){
ips=`aws ec2 describe-instances --filters Name=tag-value,Values=$1 | grep '"PrivateIpAddress"' |grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|uniq`
cssh -l ec2-user $ips -o "-i $AWS_SSH_KEY"
return
}
if [ "$1" ]
then
cssh_ec2_connect $1
exit 0
fi
PS3='Please enter your choice: '
options=()
for i in "${!ec2Tags[@]}"
do
options+=("$i")
done
options+=("Quit")
select opt in "${options[@]}"
do
if [ "$opt" ]
then
if [ ${ec2Tags[$opt]} ]
then
cssh_ec2_connect ${ec2Tags[$opt]}
break
else
break
fi
else
echo invalid option
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment