Skip to content

Instantly share code, notes, and snippets.

@fredsted
Last active May 10, 2017 08:09
Show Gist options
  • Save fredsted/41b1870c98fab48d52346b9f36641f6c to your computer and use it in GitHub Desktop.
Save fredsted/41b1870c98fab48d52346b9f36641f6c to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Installation:
# - Install `boto`: pip install -U boto
# - Configure EC2 credentials: see http://boto.cloudhackers.com/en/latest/getting_started.html#configuring-boto-credentials
# - Configure username or SSH args below
# These things are configurable
region = 'eu-central-1'
ssh_username = 'simonfredsted'
ssh_args = '-A -D1091'
import boto.ec2, sys, os
if len(sys.argv) != 2:
print 'Usage: ec2connect.py [hostname]'
sys.exit()
hostname = sys.argv[1]
instances = {}
for reservation in boto.ec2.connect_to_region(region).get_all_reservations():
for instance in reservation.instances:
instances[instance.tags['Name']] = instance.ip_address
if instances[hostname] == None:
print 'Host not found or stopped'
sys.exit()
os.system("ssh "+ ssh_args +" "+ ssh_username + "@" + instances[hostname])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment