Last active
December 18, 2015 05:29
-
-
Save trentonstrong/5732668 to your computer and use it in GitHub Desktop.
hosts.py A script for retrieving AWS EC2 instance information in a format usable for your /etc/hosts file. Poor man's DNS, anyone? Requires installation of the package 'boto' (pip install boto) and the creation of a ~/.boto configuration file explained here: http://docs.pythonboto.org/en/latest/boto_config_tut.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Usage: hosts.py [options] | |
Options: | |
-h, --help show this help message and exit | |
-r REGION, --region=REGION | |
Region (default us-east-1) | |
""" | |
import sys | |
from operator import attrgetter | |
from optparse import OptionParser | |
try: | |
import boto | |
except ImportError: | |
sys.stderr.write("Boto required. Please 'pip install boto'\n") | |
sys.exit(1) | |
from boto.ec2 import regions | |
HEADERS = { | |
'ID': {'get': attrgetter('id'), 'length':15}, | |
'Zone': {'get': attrgetter('placement'), 'length':15}, | |
'Groups': {'get': attrgetter('groups'), 'length':30}, | |
'Hostname': {'get': attrgetter('public_dns_name'), 'length':50}, | |
'PrivateHostname': {'get': attrgetter('private_dns_name'), 'length':50}, | |
'State': {'get': attrgetter('state'), 'length':15}, | |
'Image': {'get': attrgetter('image_id'), 'length':15}, | |
'Type': {'get': attrgetter('instance_type'), 'length':15}, | |
'IP': {'get': attrgetter('ip_address'), 'length':16}, | |
'PrivateIP': {'get': attrgetter('private_ip_address'), 'length':16}, | |
'Key': {'get': attrgetter('key_name'), 'length':25}, | |
'T:': {'length': 30}, | |
} | |
def get_column(name, instance=None): | |
if name.startswith('T:'): | |
_, tag = name.split(':', 1) | |
return instance.tags.get(tag, '') | |
return HEADERS[name]['get'](instance) | |
def main(): | |
parser = OptionParser() | |
parser.add_option("-r", "--region", help="Region (default us-east-1)", dest="region", default="us-east-1") | |
(options, args) = parser.parse_args() | |
# Connect the region | |
for r in regions(): | |
if r.name == options.region: | |
region = r | |
break | |
else: | |
print "Region %s not found." % options.region | |
sys.exit(1) | |
ec2 = boto.connect_ec2(region=region) | |
headers = ("IP", "T:Name") | |
# Create format string | |
format_string = "" | |
for h in headers: | |
if h.startswith('T:'): | |
format_string += "%%-%ds" % HEADERS['T:']['length'] | |
else: | |
format_string += "%%-%ds" % HEADERS[h]['length'] | |
filters = {'instance-state-name': 'running'} | |
# List and print | |
for r in ec2.get_all_instances(filters=filters): | |
groups = [g.name for g in r.groups] | |
for i in r.instances: | |
i.groups = ','.join(groups) | |
print " ".join(tuple(get_column(h, i) for h in headers)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment