Skip to content

Instantly share code, notes, and snippets.

@pecigonzalo
Last active November 13, 2019 18:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pecigonzalo/172224c21c084df518339c00a70d1373 to your computer and use it in GitHub Desktop.
Save pecigonzalo/172224c21c084df518339c00a70d1373 to your computer and use it in GitHub Desktop.
SSH bastion lookup
#!/usr/bin/env bash
set -e # exit on command errors
set -o nounset # abort on unbound variable
set -o pipefail # capture fail exit codes in piped commands
# set -x
# Config:
# ~/.ssh/config
# Host i-*
# ProxyCommand bastion %h %p
# Usage:
# ssh ec2-user@instanceID
BASTION=''
DOMAIN='yadayada.com'
REPLY=$(aws ec2 describe-instances --instance-ids "$1")
ENVIRONMENT=$(echo "$REPLY" | jq -r '.Reservations[].Instances[].Tags[] | if .Key == "Environment" then .Value else empty end')
# Replace bastion lookup logic to fit your naming scheme.
if [[ $ENVIRONMENT == "prod" ]]; then
BASTION="prod-bastion.$DOMAIN"
else
BASTION="$ENVIRONMENT-bastion.$DOMAIN"
fi
PRIVATEDNS=$(echo "$REPLY" | jq -r '.Reservations[].Instances[].PrivateDnsName')
ssh -q admin@"$BASTION" -W "$PRIVATEDNS":"$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment