Skip to content

Instantly share code, notes, and snippets.

@alangarf
Created July 7, 2016 05:02
Show Gist options
  • Save alangarf/d1613db5228f217a0f99b349d9ce7d02 to your computer and use it in GitHub Desktop.
Save alangarf/d1613db5228f217a0f99b349d9ce7d02 to your computer and use it in GitHub Desktop.
SSHs to an AWS instance with an ASG hostname
#!/bin/bash
IFS=$'\n'
unsorted_hosts=($(aws ec2 describe-instances --query "Reservations[].Instances[].{Name:Tags[?Key=='Name'].Value|[0]}" --output text --filters "Name=tag:Name,Values=*$1*"))
hosts=($(sort -V <<<"${unsorted_hosts[*]}"))
unset IFS
len=$(expr ${#hosts[@]} - 1)
for i in $(seq 0 $len); do
id=$(expr $i + 1)
echo -e "\e[31m[\e[39m$id\e[31m]\e[39m \e[33m${hosts[$i]}\e[39m"
done
echo -en "\n\e[95mSelect host:\e[39m "
read -n 3 input
case $input in
[1-9]*)
input=$(expr $input - 1)
ssh "${hosts[$input]}"
;;
*)
exit
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment