Skip to content

Instantly share code, notes, and snippets.

@cmoad
Created May 19, 2015 14:09
Show Gist options
  • Save cmoad/4be3b3800d886d62bdd6 to your computer and use it in GitHub Desktop.
Save cmoad/4be3b3800d886d62bdd6 to your computer and use it in GitHub Desktop.
AWS ssh bash script
#!/bin/bash
if [ -z $1 ]; then
echo "Usage: gssh <hostname>"
exit 1
fi
instance=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$1" | jq .Reservations[0].Instances[0])
if [ $instance == "null" ]; then
echo "Unknown host" $1
exit 1
fi
echo "Connecting to" `echo $instance | jq '.Tags[] | select(.Key == "Name") | .Value'` "..."
keyname=$(echo $instance | jq -r .KeyName)
private_ip=$(echo $instance | jq -r .PrivateIpAddress)
public_ip=$(echo $instance | jq -r .PublicIpAddress)
ssh -i ~/.ssh/${keyname}.pem ubuntu@${private_ip}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment