FindInstanceInProfile.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #! /usr/bin/python
  2. #
  3. # Find an instance within a profile, across all regions
  4. #
  5. # Unbuffered, no CRLF print:
  6. from __future__ import print_function
  7. import sys, os
  8. import boto3, boto3.session, botocore
  9. import threading # We may not do it yet, but developing with threadsafe in mind, as best I can
  10. try:
  11. # Python 3
  12. import builtins
  13. except ImportError:
  14. # Python 2
  15. import __builtin__ as builtins
  16. # Debug levels:
  17. # 1 = Show progress
  18. # 2 = Informational
  19. # 5 = Include boto3 logging
  20. DEBUG=0
  21. # Fancy print function to make it 3.4 compatible:
  22. if len(sys.argv) != 3:
  23. print("Usage: " + os.path.basename(sys.argv[0]) + " <profile> <searchstring>")
  24. exit(1)
  25. if DEBUG >= 5:
  26. boto3.set_stream_logger('botocore', level=DEBUG)
  27. PROFILE=sys.argv[1]
  28. SEARCHSTRING=sys.argv[2]
  29. # Set the profile to use
  30. try:
  31. boto3.setup_default_session(profile_name=PROFILE)
  32. except:
  33. print("Could not find profile: " + PROFILE)
  34. exit(2)
  35. # Connect to ec2
  36. ec2 = boto3.client('ec2')
  37. # Grab list of regions
  38. regions = set()
  39. for region in ec2.describe_regions()['Regions']:
  40. regions.add(region['RegionName'])
  41. # For each array, let's search:
  42. FOUND=0
  43. FOUNDSTR=""
  44. if DEBUG == 1:
  45. print("Searching.", end="")
  46. sys.stdout.flush()
  47. # Search by ID
  48. for region in regions:
  49. if DEBUG == 1:
  50. print(".", end="")
  51. sys.stdout.flush()
  52. if DEBUG >= 2:
  53. print("Searching by ID in region " + region + " in profile " + PROFILE)
  54. ec2 = boto3.client('ec2', region_name=region)
  55. try:
  56. instance = ec2.describe_instances(InstanceIds=[ SEARCHSTRING ])
  57. except botocore.exceptions.ClientError:
  58. # Not Found
  59. continue
  60. except:
  61. print(str( sys.exc_info() ))
  62. continue
  63. FOUND=FOUND+1
  64. if DEBUG >= 2:
  65. print("FOUND in profile '" + PROFILE + "', Region: '" + region + "'")
  66. if FOUND > 1:
  67. FOUNDSTR = FOUNDSTR + "\n"
  68. # Add to output
  69. for r in instance['Reservations']:
  70. for i in r['Instances']:
  71. FOUNDSTR = FOUNDSTR + "FOUND\t" + PROFILE + "\t" + region + "\t" + i['InstanceId']
  72. continue # Search next region by ID
  73. # Search by Name
  74. for region in regions:
  75. if DEBUG == 1:
  76. print(".", end="")
  77. sys.stdout.flush()
  78. if DEBUG >= 2:
  79. print("Searching by Name in region " + region + " in profile " + PROFILE)
  80. ec2 = boto3.client('ec2', region_name=region)
  81. try:
  82. instance = ec2.describe_instances(Filters=[ {'Name': 'tag:Name', 'Values': [ SEARCHSTRING ] } ])
  83. except:
  84. print(str( sys.exc_info() ))
  85. # Add to output
  86. for r in instance['Reservations']:
  87. for i in r['Instances']:
  88. FOUND=FOUND+1
  89. if DEBUG >= 2:
  90. print("FOUND in profile '" + PROFILE + "', Region: '" + region + "'")
  91. if FOUND > 1:
  92. FOUNDSTR = FOUNDSTR + "\n"
  93. FOUNDSTR = FOUNDSTR + "FOUND\t" + PROFILE + "\t" + region + "\t" + i['InstanceId']
  94. continue # Search next region by name
  95. # End of for region
  96. if DEBUG == 1:
  97. print(".")
  98. sys.stdout.flush()
  99. if DEBUG >= 2:
  100. print("Found " + str(FOUND) + " instances.")
  101. if FOUND > 0:
  102. print(FOUNDSTR)
  103. exit(0)
  104. else:
  105. if DEBUG == 1:
  106. print("notfound")
  107. exit(255)