فهرست منبع

Output fixed, can search instance by name

Fred Damstra 9 سال پیش
والد
کامیت
7e81502ddd
1فایلهای تغییر یافته به همراه33 افزوده شده و 2 حذف شده
  1. 33 2
      FindInstanceInProfile.py

+ 33 - 2
FindInstanceInProfile.py

@@ -53,12 +53,14 @@ FOUNDSTR=""
 if DEBUG == 1:
 	print("Searching.", end="")
 	sys.stdout.flush()
+
+# Search by ID
 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)
 	ec2 = boto3.client('ec2', region_name=region)
 	try:
 		instance = ec2.describe_instances(InstanceIds=[ SEARCHSTRING ])
@@ -67,6 +69,7 @@ for region in regions:
 		continue
 	except:
 		print(str( sys.exc_info() ))
+		continue
 
 	FOUND=FOUND+1
 	if DEBUG >= 2:
@@ -77,7 +80,35 @@ for region in regions:
 	# Add to output
 	for r in instance['Reservations']:
 		for i in r['Instances']:
-			FOUNDSTR = "FOUND\t" + PROFILE + "\t" + region + "\t" + i['InstanceId']
+			FOUNDSTR = FOUNDSTR + "FOUND\t" + PROFILE + "\t" + region + "\t" + i['InstanceId']
+
+	continue	# Search next region by ID
+
+# Search by Name
+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)
+
+	ec2 = boto3.client('ec2', region_name=region)
+	try:
+		instance = ec2.describe_instances(Filters=[ {'Name': 'tag:Name', 'Values': [ SEARCHSTRING ] } ])
+	except:
+		print(str( sys.exc_info() ))
+
+	# Add to output
+	for r in instance['Reservations']:
+		for i in r['Instances']:
+			FOUND=FOUND+1
+			if DEBUG >= 2:
+				print("FOUND in profile '" + PROFILE + "', Region: '" + region + "'")
+			if FOUND > 1:
+				FOUNDSTR = FOUNDSTR + "\n"
+			FOUNDSTR = FOUNDSTR + "FOUND\t" + PROFILE + "\t" + region + "\t" + i['InstanceId']
+
+	continue	# Search next region by name
 
 # End of for region	
 if DEBUG == 1: