FindSecurityGroupInProfile.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. # 3 = Additional error info (includes failed lookups)
  20. # 5 = Include boto3 logging
  21. DEBUG=0
  22. # Fancy print function to make it 3.4 compatible:
  23. if len(sys.argv) != 3:
  24. print("Usage: " + os.path.basename(sys.argv[0]) + " <profile> <searchstring>")
  25. exit(1)
  26. if DEBUG >= 5:
  27. boto3.set_stream_logger('botocore', level=DEBUG)
  28. PROFILE=sys.argv[1]
  29. SEARCHSTRING=sys.argv[2]
  30. # Set the profile to use
  31. try:
  32. boto3.setup_default_session(profile_name=PROFILE)
  33. except:
  34. print("Could not find profile: " + PROFILE)
  35. exit(2)
  36. # Connect to ec2
  37. ec2 = boto3.client('ec2')
  38. # Grab list of regions
  39. regions = set()
  40. for region in ec2.describe_regions()['Regions']:
  41. regions.add(region['RegionName'])
  42. # For each array, let's search:
  43. FOUND=0
  44. FOUNDSTR=""
  45. if DEBUG == 1:
  46. print("Searching.", end="")
  47. sys.stdout.flush()
  48. # Search by ID in each region
  49. for region in regions:
  50. if DEBUG == 1:
  51. print(".", end="")
  52. sys.stdout.flush()
  53. if DEBUG >= 2:
  54. print("Searching by ID in region " + region + " in profile " + PROFILE)
  55. # Connect to region
  56. ec2 = boto3.client('ec2', region_name=region)
  57. # Search by ID
  58. try:
  59. sg = ec2.describe_security_groups(GroupIds=[ SEARCHSTRING ])
  60. except botocore.exceptions.ClientError as e:
  61. # Not found by ID
  62. continue
  63. except:
  64. # Print the error
  65. print(str( sys.exc_info() ))
  66. continue
  67. # If we're here, we found at least one
  68. # Add to output
  69. for g in sg['SecurityGroups']:
  70. FOUND=FOUND+1
  71. if DEBUG >= 2:
  72. print("FOUND in profile '" + PROFILE + "', Region: '" + region + "': ID=" + str(sg['SecurityGroups'][0]['GroupId']))
  73. if FOUND > 1:
  74. FOUNDSTR = FOUNDSTR + "\n"
  75. FOUNDSTR = FOUNDSTR + "FOUND\t" + PROFILE + "\t" + region + "\t" + str(g['GroupId'])
  76. continue # Search next region by ID
  77. # Search by Name in each region
  78. for region in regions:
  79. if DEBUG == 1:
  80. print(".", end="")
  81. sys.stdout.flush()
  82. if DEBUG >= 2:
  83. print("Searching by Name in region " + region + " in profile " + PROFILE)
  84. # Connect to region
  85. ec2 = boto3.client('ec2', region_name=region)
  86. # Search by ID
  87. try:
  88. sg = ec2.describe_security_groups(Filters=[ {'Name': 'group-name', 'Values': [ SEARCHSTRING ] } ])
  89. except:
  90. # Print the error
  91. print(str( sys.exc_info() ))
  92. # If we're here, we got a result
  93. for g in sg['SecurityGroups']:
  94. FOUND=FOUND+1
  95. if DEBUG >= 2:
  96. print("FOUND in profile '" + PROFILE + "', Region: '" + region + "': ID=" + str(sg['SecurityGroups'][0]['GroupId']))
  97. if FOUND > 1:
  98. FOUNDSTR = FOUNDSTR + "\n"
  99. FOUNDSTR = FOUNDSTR + "FOUND\t" + PROFILE + "\t" + region + "\t" + str(g['GroupId'])
  100. continue # Search next region by name
  101. # End of for region
  102. if DEBUG == 1:
  103. print(".")
  104. sys.stdout.flush()
  105. if DEBUG >= 2:
  106. print("Found " + str(FOUND) + " instances.")
  107. if FOUND > 0:
  108. print(FOUNDSTR)
  109. exit(0)
  110. else:
  111. if DEBUG == 1:
  112. print("Not found.")
  113. exit(255)