Skip to content

Instantly share code, notes, and snippets.

@colinvh
Last active October 26, 2016 19:38
Show Gist options
  • Save colinvh/c65a8de43e46d461561a4e196b06677b to your computer and use it in GitHub Desktop.
Save colinvh/c65a8de43e46d461561a4e196b06677b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
verbose=false
profile=
region=
while [[ "$1" == -* ]] ; do
if [ "$1" == -h ] ; then
echo "usage: $(basename $0) [-p PROFILE] [-r REGION] [-v] instance-id"
exit
elif [ "$1" == -p ] ; then
profile="--profile $2"
shift ; shift
elif [ "$1" == -r ] ; then
region="--region $2"
shift ; shift
elif [ "$1" == -v ] ; then
verbose=true
shift
else
echo "$(basename $0): unrecognized argument $1" >&2
exit 1
fi
done
id="$1"
shift
read -r ip vpc <<<"$(aws ec2 describe-instances $profile $region --filter Name=instance-id,Values=$id | jq -r '.Reservations[0].Instances[0] | "\(.PrivateIpAddress) \(.VpcId)"')"
if $verbose ; then
echo connecting to $ip
fi
if [ $vpc == null ] ; then
ssh $ip $@
else
vpcip=$(aws ec2 describe-instances $profile $region --filter Name=tag:group,Values=jump Name=vpc-id,Values=$vpc | jq -r '.Reservations[0].Instances[0].PublicIpAddress')
if $verbose ; then
echo connecting via $vpcip
fi
ssh -At $vpcip ssh $ip $@
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment