Last active
November 13, 2019 18:02
-
-
Save pecigonzalo/172224c21c084df518339c00a70d1373 to your computer and use it in GitHub Desktop.
SSH bastion lookup
This file contains 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
#!/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