浏览代码

FindSecurityGroupInProfile fixed

Fred Damstra 9 年之前
父节点
当前提交
39b55b6f49
共有 2 个文件被更改,包括 59 次插入31 次删除
  1. 5 1
      FindInstanceInProfile.py
  2. 54 30
      FindSecurityGroupInProfile.py

+ 5 - 1
FindInstanceInProfile.py

@@ -65,12 +65,16 @@ for region in regions:
 	except botocore.exceptions.ClientError:
 		# Not Found
 		continue
-	
+	except:
+		print(str( sys.exc_info() ))
+
 	FOUND=FOUND+1
 	if DEBUG >= 2:
 		print("FOUND in profile '" + PROFILE + "', Region: '" + region + "'")
 	if FOUND > 1:
 		FOUNDSTR = FOUNDSTR + "\n"
+
+	# Add to output
 	for r in instance['Reservations']:
 		for i in r['Instances']:
 			FOUNDSTR = "FOUND\t" + PROFILE + "\t" + region + "\t" + i['InstanceId']

+ 54 - 30
FindSecurityGroupInProfile.py

@@ -5,9 +5,7 @@
 # Unbuffered, no CRLF print:
 from __future__ import print_function
 import sys, os
-import boto3
-import boto3.session
-import botocore
+import boto3, boto3.session, botocore
 import threading 	# We may not do it yet, but developing with threadsafe in mind, as best I can
 
 try:
@@ -56,47 +54,73 @@ FOUNDSTR=""
 if DEBUG == 1:
 	print("Searching.", end="")
 	sys.stdout.flush()
+
+# Search by ID in each region
 for region in regions:
 	if DEBUG == 1:
 		print(".", end="")
 		sys.stdout.flush()
 	if DEBUG >= 2:
-		print("Searching region " + region + " in profile " + PROFILE)
+		print("Searching by ID in region " + region + " in profile " + PROFILE)
+
+	# Connect to region
 	ec2 = boto3.client('ec2', region_name=region)
+
+	# Search by ID
 	try:
 		sg = ec2.describe_security_groups(GroupIds=[ SEARCHSTRING ])
-		FOUND=FOUND+1
-		if DEBUG >= 2:
-			print("FOUND in profile '" + PROFILE + "', Region: '" + region + "': ID=" + str(sg['SecurityGroups'][0]['GroupId']))
-		if FOUND > 1:
-			FOUNDSTR = FOUNDSTR + "\n"
-		FOUNDSTR = FOUNDSTR + "FOUND in profile '" + PROFILE + "', Region: '" + region + "'; ID=" + str(sg['SecurityGroups'][0]['GroupId'])
-		continue
 	except botocore.exceptions.ClientError as e:
-		if DEBUG >= 2:
-			print("Not found by ID in profile '" + PROFILE + "', Region: '" + region + "'")
+		# Not found by ID
+		continue
 	except:
-		if DEBUG >= 3:
-			# Print the error
-			print(str( sys.exc_info() ))
+		# Print the error
+		print(str( sys.exc_info() ))
+		continue
+
+	# If we're here, we found at least one
+	FOUND=FOUND+1
+	if DEBUG >= 2:
+		print("FOUND in profile '" + PROFILE + "', Region: '" + region + "': ID=" + str(sg['SecurityGroups'][0]['GroupId']))
+	if FOUND > 1:
+		FOUNDSTR = FOUNDSTR + "\n"
+
+	# Add to output
+	for g in sg['SecurityGroups']:
+		FOUNDSTR = FOUNDSTR + "FOUND\t" + PROFILE + "\t" + region + "\t" + str(g['GroupId'])
+
+	continue	# Search next region by ID
+
+
+# Search by Name in each region
+for region in regions:
+	if DEBUG == 1:
+		print(".", end="")
+		sys.stdout.flush()
+	if DEBUG >= 2:
+		print("Searching by Name in region " + region + " in profile " + PROFILE)
+
+	# Connect to region
+	ec2 = boto3.client('ec2', region_name=region)
+
+	# Search by ID
 	try:
 		sg = ec2.describe_security_groups(Filters=[ {'Name': 'group-name', 'Values': [ SEARCHSTRING ] } ])
 	except:
-		if DEBUG >= 3:
-			# Print the error
-			print(str( sys.exc_info() ))
-	try:
-		if(sg['SecurityGroups'][0]['GroupId']):
-			FOUND=FOUND+1
-			if DEBUG >= 2:
-				print("FOUND in profile '" + PROFILE + "', Region: '" + region + "': ID=" + str(sg['SecurityGroups'][0]['GroupId']))
-			if FOUND > 1:
-				FOUNDSTR = FOUNDSTR + "\n"
-		        FOUNDSTR = FOUNDSTR + "FOUND in profile '" + PROFILE + "', Region: '" + region + "'; ID=" + str(sg['SecurityGroups'][0]['GroupId'])
-			continue
-	except IndexError:
+		# Print the error
+		print(str( sys.exc_info() ))
+
+	# If we're here, we got a result
+	for g in sg['SecurityGroups']:
+		print("-----\n" + str(g) + "\n-----\n")
+#		FOUNDSTR = "FOUND\t" + PROFILE + "\t" + region + "\t" + str(g['GroupId'])
+		FOUND=FOUND+1
 		if DEBUG >= 2:
-			print("Not found by Name search in profile '" + PROFILE + "', Region: '" + region + "'")
+			print("FOUND in profile '" + PROFILE + "', Region: '" + region + "': ID=" + str(sg['SecurityGroups'][0]['GroupId']))
+		if FOUND > 1:
+			FOUNDSTR = FOUNDSTR + "\n"
+		FOUNDSTR = FOUNDSTR + "FOUND\t" + PROFILE + "\t" + region + "\t" + str(g['GroupId'])
+	
+	continue	# Search next region by name
 
 # End of for region	
 if DEBUG == 1: