123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #! /usr/bin/python
- #
- # Find an instance within a profile, across all regions
- #
- # Unbuffered, no CRLF print:
- from __future__ import print_function
- import sys, os
- import boto3, boto3.session, botocore
- import threading # We may not do it yet, but developing with threadsafe in mind, as best I can
- try:
- # Python 3
- import builtins
- except ImportError:
- # Python 2
- import __builtin__ as builtins
- # Debug levels:
- # 1 = Show progress
- # 2 = Informational
- # 3 = Additional error info (includes failed lookups)
- # 5 = Include boto3 logging
- DEBUG=0
- # Fancy print function to make it 3.4 compatible:
- if len(sys.argv) != 3:
- print("Usage: " + os.path.basename(sys.argv[0]) + " <profile> <searchstring>")
- exit(1)
- if DEBUG >= 5:
- boto3.set_stream_logger('botocore', level=DEBUG)
- PROFILE=sys.argv[1]
- SEARCHSTRING=sys.argv[2]
- # Set the profile to use
- try:
- boto3.setup_default_session(profile_name=PROFILE)
- except:
- print("Could not find profile: " + PROFILE)
- exit(2)
- # Connect to ec2
- ec2 = boto3.client('ec2')
- # Grab list of regions
- regions = set()
- for region in ec2.describe_regions()['Regions']:
- regions.add(region['RegionName'])
- # For each array, let's search:
- FOUND=0
- 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 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 ])
- except botocore.exceptions.ClientError as e:
- # Not found by ID
- continue
- except:
- # Print the error
- print(str( sys.exc_info() ))
- continue
- # If we're here, we found at least one
- # Add to output
- for g in sg['SecurityGroups']:
- 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\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:
- # Print the error
- print(str( sys.exc_info() ))
- # If we're here, we got a result
- for g in sg['SecurityGroups']:
- 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\t" + PROFILE + "\t" + region + "\t" + str(g['GroupId'])
-
- continue # Search next region by name
- # End of for region
- if DEBUG == 1:
- print(".")
- sys.stdout.flush()
- if DEBUG >= 2:
- print("Found " + str(FOUND) + " instances.")
- if FOUND > 0:
- print(FOUNDSTR)
- exit(0)
- else:
- if DEBUG == 1:
- print("Not found.")
- exit(255)
|