Skip to content

Instantly share code, notes, and snippets.

@daniel-woods
Created September 9, 2018 10:12
Show Gist options
  • Save daniel-woods/58fc74ca21a45ea86b56982529e4c6ed to your computer and use it in GitHub Desktop.
Save daniel-woods/58fc74ca21a45ea86b56982529e4c6ed to your computer and use it in GitHub Desktop.
Use the AWSCLI to parse the Public IP from a describe-instances call to connect via SSH.
#!/bin/bash
# Default user uses different names, depending on OS.
# Amazon Linux = "ec2-user"
# Ubuntu = "ubuntu"
remote_user="ec2-user"
pem="/home/.ssh/YOUR_PRIVATE_KEY"
port=22
# Get this using describe_instances, or via the AWS Console.
instance="i-YOUR_AWS_INSTANCE_ID"
ssh $(aws ec2 describe-instances --instance-id $instance --query Reservations[].Instances[].NetworkInterfaces[].Association[].PublicIp[] | cut -c 6- | rev | cut -c 2- | rev) -l $remote_user -i $pem -p $port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment