FindSecurityGroupInProfile.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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
  9. import boto3.session
  10. import botocore
  11. import threading # We may not do it yet, but developing with threadsafe in mind, as best I can
  12. try:
  13. # Python 3
  14. import builtins
  15. except ImportError:
  16. # Python 2
  17. import __builtin__ as builtins
  18. # Debug levels:
  19. # 1 = Show progress
  20. # 2 = Informational
  21. # 3 = Additional error info (includes failed lookups)
  22. # 5 = Include boto3 logging
  23. DEBUG=0
  24. # Fancy print function to make it 3.4 compatible:
  25. if len(sys.argv) != 3:
  26. print("Usage: " + os.path.basename(sys.argv[0]) + "<searchstring> <profile>")
  27. exit(1)
  28. if DEBUG >= 5:
  29. boto3.set_stream_logger('botocore', level=DEBUG)
  30. SEARCHSTRING=sys.argv[1]
  31. PROFILE=sys.argv[2]
  32. # Set the profile to use
  33. try:
  34. boto3.setup_default_session(profile_name=PROFILE)
  35. except:
  36. print("Could not find profile: " + PROFILE)
  37. exit(2)
  38. # Connect to ec2
  39. ec2 = boto3.client('ec2')
  40. # Grab list of regions
  41. regions = set()
  42. for region in ec2.describe_regions()['Regions']:
  43. regions.add(region['RegionName'])
  44. # For each array, let's search:
  45. FOUND=0
  46. FOUNDSTR=""
  47. if DEBUG == 1:
  48. print("Searching.", end="")
  49. sys.stdout.flush()
  50. for region in regions:
  51. if DEBUG == 1:
  52. print(".", end="")
  53. sys.stdout.flush()
  54. if DEBUG >= 2:
  55. print("Searching region " + region + " in profile " + PROFILE)
  56. ec2 = boto3.client('ec2', region_name=region)
  57. try:
  58. sg = ec2.describe_security_groups(GroupIds=[ SEARCHSTRING ])
  59. FOUND=FOUND+1
  60. if DEBUG >= 2:
  61. print("FOUND in profile '" + PROFILE + "', Region: '" + region + "': ID=" + str(sg['SecurityGroups'][0]['GroupId']))
  62. if FOUND > 1:
  63. FOUNDSTR = FOUNDSTR + "\n"
  64. FOUNDSTR = FOUNDSTR + "FOUND in profile '" + PROFILE + "', Region: '" + region
  65. continue
  66. except botocore.exceptions.ClientError as e:
  67. if DEBUG >= 2:
  68. print("Not found by ID in profile '" + PROFILE + "', Region: '" + region + "'")
  69. except:
  70. if DEBUG >= 3:
  71. # Print the error
  72. print(str( sys.exc_info() ))
  73. try:
  74. sg = ec2.describe_security_groups(Filters=[ {'Name': 'group-name', 'Values': [ SEARCHSTRING ] } ])
  75. except:
  76. if DEBUG >= 3:
  77. # Print the error
  78. print(str( sys.exc_info() ))
  79. try:
  80. if(sg['SecurityGroups'][0]['GroupId']):
  81. FOUND=FOUND+1
  82. if DEBUG >= 2:
  83. print("FOUND in profile '" + PROFILE + "', Region: '" + region + "': ID=" + str(sg['SecurityGroups'][0]['GroupId']))
  84. if FOUND > 1:
  85. FOUNDSTR = FOUNDSTR + "\n"
  86. FOUNDSTR = FOUNDSTR + "FOUND in profile '" + PROFILE + "', Region: '" + region
  87. continue
  88. except IndexError:
  89. if DEBUG >= 2:
  90. print("Not found by Name search in profile '" + PROFILE + "', Region: '" + region + "'")
  91. # End of for region
  92. if DEBUG == 1:
  93. print(".")
  94. sys.stdout.flush()
  95. if DEBUG >= 2:
  96. print("Found " + str(FOUND) + " instances.")
  97. if FOUND > 0:
  98. print(FOUNDSTR)
  99. exit(0)
  100. else:
  101. if DEBUG == 1:
  102. print("Not found.")
  103. exit(255)