You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.1 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  2. import sys
  3. from operator import attrgetter
  4. from optparse import OptionParser
  5. import boto
  6. from boto.ec2 import regions
  7. HEADERS = {
  8. 'ID': {'get': attrgetter('id'), 'length':15},
  9. 'Zone': {'get': attrgetter('placement'), 'length':15},
  10. 'Groups': {'get': attrgetter('groups'), 'length':30},
  11. 'Hostname': {'get': attrgetter('public_dns_name'), 'length':50},
  12. 'PrivateHostname': {'get': attrgetter('private_dns_name'), 'length':50},
  13. 'State': {'get': attrgetter('state'), 'length':15},
  14. 'Image': {'get': attrgetter('image_id'), 'length':15},
  15. 'Type': {'get': attrgetter('instance_type'), 'length':15},
  16. 'IP': {'get': attrgetter('ip_address'), 'length':16},
  17. 'PrivateIP': {'get': attrgetter('private_ip_address'), 'length':16},
  18. 'Key': {'get': attrgetter('key_name'), 'length':25},
  19. 'T:': {'length': 30},
  20. }
  21. def get_column(name, instance=None):
  22. if name.startswith('T:'):
  23. _, tag = name.split(':', 1)
  24. return instance.tags.get(tag, '')
  25. return HEADERS[name]['get'](instance)
  26. def main():
  27. parser = OptionParser()
  28. parser.add_option("-r", "--region", help="Region (default us-east-1)", dest="region", default="us-east-1")
  29. parser.add_option("-H", "--headers", help="Set headers (use 'T:tagname' for including tags)", default=None, action="store", dest="headers", metavar="ID,Zone,Groups,Hostname,State,T:Name")
  30. parser.add_option("-t", "--tab", help="Tab delimited, skip header - useful in shell scripts", action="store_true", default=False)
  31. parser.add_option("-f", "--filter", help="Filter option sent to DescribeInstances API call, format is key1=value1,key2=value2,...", default=None)
  32. (options, args) = parser.parse_args()
  33. # Connect the region
  34. for r in regions():
  35. if r.name == options.region:
  36. region = r
  37. break
  38. else:
  39. print("Region %s not found." % options.region)
  40. sys.exit(1)
  41. ec2 = boto.connect_ec2(region=region)
  42. # Read headers
  43. if options.headers:
  44. headers = tuple(options.headers.split(','))
  45. else:
  46. headers = ("ID", 'Zone', "Groups", "Hostname")
  47. # Create format string
  48. format_string = ""
  49. for h in headers:
  50. if h.startswith('T:'):
  51. format_string += "%%-%ds" % HEADERS['T:']['length']
  52. else:
  53. format_string += "%%-%ds" % HEADERS[h]['length']
  54. # Parse filters (if any)
  55. if options.filter:
  56. filters = dict([entry.split('=') for entry in options.filter.split(',')])
  57. else:
  58. filters = {}
  59. # List and print
  60. if not options.tab:
  61. print(format_string % headers)
  62. print("-" * len(format_string % headers))
  63. for r in ec2.get_all_reservations(filters=filters):
  64. groups = [g.name for g in r.groups]
  65. for i in r.instances:
  66. i.groups = ','.join(groups)
  67. if options.tab:
  68. print("\t".join(tuple(get_column(h, i) for h in headers)))
  69. else:
  70. print(format_string % tuple(get_column(h, i) for h in headers))
  71. if __name__ == "__main__":
  72. main()