Skip to content

Instantly share code, notes, and snippets.

@mijndert
Created June 1, 2021 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mijndert/6ea3edc80300a6185dfee88111e46b0c to your computer and use it in GitHub Desktop.
Save mijndert/6ea3edc80300a6185dfee88111e46b0c to your computer and use it in GitHub Desktop.
#!/bin/bash
green=`tput setaf 2`
reset=`tput sgr0`
echo "${green}Hostname of the resource you want to connect to:${reset}"
read -e hostname
echo "${green}Port of the thing you want to connect to:${reset}"
read -e port
echo "${green}Connecting (quit with ctrl+c)${reset}"
export ID=$(aws ec2 describe-instances --output=text --query "Reservations[*].Instances[*].InstanceId" --filters Name=tag:Name,Values="BastionStack*")
export AZ=$(aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[Placement.AvailabilityZone]' --filters Name=tag:Name,Values="BastionStack*")
aws ec2-instance-connect \
send-ssh-public-key \
--availability-zone $AZ \
--instance-id $ID \
--instance-os-user ssm-user \
--ssh-public-key file://$HOME/.ssh/id_rsa.pub &>/dev/null & disown;
ssh ssm-user@$ID -NL $port\:$hostname\:$port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment