Skip to content

Instantly share code, notes, and snippets.

@enriquemanuel
Last active October 31, 2018 20:37
Show Gist options
  • Select an option

  • Save enriquemanuel/bd9b0852563f788d545af1ef160af138 to your computer and use it in GitHub Desktop.

Select an option

Save enriquemanuel/bd9b0852563f788d545af1ef160af138 to your computer and use it in GitHub Desktop.
Connect to the Spoke VPC automatically
#!/bin/bash
#input vars:
# stage: [prod, staging]
# aws_profile: [default]
bold=$(tput bold)
normal=$(tput sgr0)
USAGE=$(cat <<-END
getIntoSpoke.sh [STAGE] [AWS_PROFILE]
Gets the IP of the Jumpbox, Non Spoke StrongSwan and Spoke StrongSwan and starts a connection to the last point for you for the stage [prod or staging]
END
)
if ! [ -x "$(command -v jq)" ]; then
echo '${bold}Error: jq ${normal}is not installed.' >&2
exit 1
fi
if ! [ -x "$(command -v aws)" ]; then
echo '${bold}Error: aws-cli ${normal}is not installed.' >&2
exit 1
fi
if [[ $1 == "-h" ]]; then
echo "$USAGE"
exit 0
fi
if [[ $# -ne 2 ]]; then
echo "$USAGE"
exit 1
fi
echo "Confirming Variables..."
profile_exists=$(grep -c $2 ~/.aws/config)
if [ $profile_exists -ne 1 ]; then
echo "AWS Profile doesn't exist. Stopping script"
exit 1
fi
echo "Getting Different IP's"
# Get Jumpbox
jumpbox_ip=$(aws ec2 --profile $2 --region us-gov-west-1 describe-instances --filters "Name=tag:Name,Values=dsva-appeals-$1-jumpbox-ec2" --query "Reservations[*].Instances[*].NetworkInterfaces[*].Association" | jq -r '.[][][0].PublicIp')
if [ ${#jumpbox_ip} -eq 0 ]; then
echo ${bold}"Error getting Jumpbox IP. ${normal}Stopping Script"
exit 1
fi
echo "${bold}JUMPBOX IP: ${normal} $jumpbox_ip"
# Non Spoke VPC
strongswan_non_vpc=$(aws ec2 --profile $2 --region us-gov-west-1 describe-instances --filters "Name=tag:Name,Values=dsva-appeals-$1-strongswan-1a-ec2" --query "Reservations[*].Instances[*].NetworkInterfaces[*].PrivateIpAddresses" | jq -r '.[][][][].PrivateIpAddress')
if [ ${#strongswan_non_vpc} -eq 0 ]; then
echo "${bold}Error getting Non Spoke IP. ${normal}Stopping Script"
exit 1
fi
echo "${bold}StrongSwan Non Spoke IP: ${normal} $strongswan_non_vpc"
# Spoke VPC
strongswan_vpc=$(aws ec2 --profile $2 --region us-gov-west-1 describe-instances --filters "Name=tag:Name,Values=dsva-appeals-spoke-$1-strongswan-spoke-1a-ec2" --query "Reservations[*].Instances[*].NetworkInterfaces[*].PrivateIpAddresses" | jq -r '.[][][][].PrivateIpAddress')
if [ ${#strongswan_vpc} -eq 0 ]; then
echo "${bold}Error getting Spoke IP. ${normal}Stopping Script"
exit 1
fi
echo "${bold}StrongSwan Spoke IP: ${normal} $strongswan_vpc"
# Validate the configuration exists in the .ssh/config
identity_exists=$(grep -A5 $jumpbox_ip ~/.ssh/config | grep -c IdentityFile)
if [ $identity_exists -ne 1 ]; then
echo "${bold}Error: ${normal}Identity File doesn't exist for this configuration..."
exit 1
fi
# Adding Identity File
identity_file=$(grep -A5 $jumpbox_ip ~/.ssh/config | grep IdentityFile | awk '{print $3}' )
echo "Adding identity to the keystore"
ssh-add $identity_file 2>/dev/null
#connecting
echo "Connecting..."
ssh -tA dsva@$jumpbox_ip ssh -tA dsva@$strongswan_non_vpc ssh dsva@$strongswan_vpc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment