Skip to content

Instantly share code, notes, and snippets.

@neersighted
Forked from quiver/eic-cli.sh
Created May 9, 2022 21:52
Show Gist options
  • Save neersighted/e43cae66dada6749ed41c3adebc16d93 to your computer and use it in GitHub Desktop.
Save neersighted/e43cae66dada6749ed41c3adebc16d93 to your computer and use it in GitHub Desktop.
simple shell script to demonstrate how EC2 Instance Connect CLI is implemented
#!/bin/bash
# simple shell script to demonstrate how EC2 Instance Connect CLI is implemented.
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstances.html
#
# Usage
# $ bash eic-cli.sh i-1234
if [ $# -ne 1 ]; then
echo "Usage"
echo "$ bash eic-cli.sh i-1234"
exit 1
fi
instance_id=$1
# get EC2 data
availability_zone=$(aws ec2 describe-instances --instance-ids $instance_id | jq -r .Reservations[0].Instances[0].Placement.AvailabilityZone)
ip_address=$(aws ec2 describe-instances --instance-ids $instance_id | jq -r .Reservations[0].Instances[0].PublicIpAddress)
# generate RSA key pair
tmpfile=$(mktemp /tmp/ssh.XXXXXX)
ssh-keygen -C "eic temp key" -q -f $tmpfile -t rsa -b 2048 -N ""
public_key=${tmpfile}.pub
private_key=$tmpfile
# register public key
aws ec2-instance-connect send-ssh-public-key \
--instance-id $instance_id \
--instance-os-user ec2-user \
--ssh-public-key file://$public_key \
--availability-zone $availability_zone
# ssh into ec2 instance with private key
ssh -i $private_key ec2-user@$ip_address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment