Skip to content

Instantly share code, notes, and snippets.

@jahil
Created November 4, 2014 12:25
Show Gist options
  • Save jahil/2fa461f16d0ab62ea6b1 to your computer and use it in GitHub Desktop.
Save jahil/2fa461f16d0ab62ea6b1 to your computer and use it in GitHub Desktop.
AWS Report Tool
__author__ = 'mohsin'
import boto.ec2
import optparse
from termcolor import colored
parser = optparse.OptionParser()
parser.add_option("-r", action="count", dest="regions", help='list all regions')
parser.add_option("-i", action="count", dest="instances", help='list all instances in region')
parser.add_option("-a", action="count", dest="all_instances", help='list all instances in all region')
parser.add_option("-s", action="count", dest="snapshots", help='list all snapshots in region')
options, args = parser.parse_args()
ec2 = boto.ec2.connect_to_region('us-west-2')
allregions = ["ap-southeast-1","ap-southeast-2","us-west-2","us-east-1","us-west-1","sa-east-1","ap-northeast-1","eu-west-1"]
'''
def endpoints():
aws_regions = boto.ec2.regions()
for i in aws_regions:
x = str(i).split(':', 1)
print x[1]
if options.regions:
endpoints()
'''
if options.snapshots:
snaps = ec2.get_all_snapshots(owner='347644749616')
for i in snaps:
try:
print '%s \t %s \t %s' % (i.start_time, i.id, i.description)
except Exception, e:
print e
if options.instances:
reservation = ec2.get_all_instances()
for i in reservation:
instance = i.instances[0]
def fprint(instance):
#if not instance.tags.has_key('Owner'):
#print '%s \t %s \t %s \t %s \t %s \t %s' % (instance.id, instance.state, instance.instance_type, instance.region, instance.tags['Name'], instance.ip_address)
#print '%s \t %s \t %s \t %s \t \t %s' % (instance.id, instance.state, instance.instance_type, instance.ip_address, instance.tags['Name'])
if instance.state == 'running':
print str(instance.id), '\t', colored(instance.state,'green'),'\t', str(instance.instance_type),'\t', str(instance.ip_address), '\t', str(instance.tags['Name'])
else :
print str(instance.id),'\t', colored(instance.state,'red'),'\t', str(instance.instance_type),'\t', str(instance.ip_address), '\t', str(instance.tags['Name'])
#print (instance.id, instance.state, instance.instance_type, instance.ip_address, instance.tags['Name']
regions = {}
if options.all_instances:
for regionx in allregions:
connect = boto.ec2.connect_to_region(regionx)
print connect
regions[regionx] = connect.get_all_instances()
for region in regions:
print '======================== ' + region + ' ========================='
for reservation in regions[region]:
fprint(reservation.instances[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment