Created
April 20, 2018 11:33
-
-
Save jinnko/2140d2faed6e14580024e49cda7a2533 to your computer and use it in GitHub Desktop.
SSH to AWS EC2 instances behind a bastion, using ec2-ssh
This file contains hidden or 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/sh | |
usage() { | |
echo "Usage: $(basename "$0") [-h|--help] [-J USER@HOST] [-l user] [ssh-opts] \$ec2-host" | |
} | |
help() { | |
usage | |
echo | |
echo "SSH to an EC2 host determined by the Name tag, via a bastion host." | |
echo | |
echo "Options:" | |
echo "-J USER@HOST Connection info for the bastion server, defaults to 'ec2-user@bastion'." | |
echo "-l USER SSH user to connect as to the final host, defaults to 'ec2-user'." | |
echo | |
echo "Where:" | |
echo " \$ec2-host is a tag that will be resolved by the ec2-host tool from which" | |
echo " the first entry will be used for the connection." | |
} | |
shortoptions='hJ:l:' | |
longoptions='help,jump:,login:' | |
getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@") | |
if [ $? != 0 ] || [ $# -eq 0 ]; then | |
usage | |
exit 1; | |
fi | |
eval set -- "$getopt" | |
while true; do | |
case "$1" in | |
-h|--help) help; exit 1;; | |
-J|--jump) BASTION=$2; shift 2;; | |
-l|--login) LOGIN=$2; shift 2;; | |
--) shift; break;; | |
esac | |
done | |
if [ -z "$1" ]; then | |
help; exit 1 | |
fi | |
EC2_HOST=$1 | |
shift | |
[ -z "$BASTION" ] && BASTION="ec2-user@bastion" | |
[ -z "$LOGIN" ] && LOGIN="ec2-user" | |
ssh -o "ProxyCommand ec2-ssh $BASTION nc %h %p" -l $LOGIN $SSH_OPTS $@ $(ec2-host $EC2_HOST | head -n1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment