Created
September 21, 2020 05:57
-
-
Save Burekasim/bc033bf4ee78b722f9e61792f2f17ae1 to your computer and use it in GitHub Desktop.
List all the ec2 instances from all the regions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import sys | |
import boto3 | |
from queue import Queue | |
from threading import Thread | |
from tabulate import tabulate | |
class Aws: | |
def __init__(self, region='us-east-1', aws_profile='default'): | |
self.region = region | |
self.session = boto3.Session(profile_name=aws_profile) | |
def get_regions(self): | |
return [region['RegionName'] for region in self.session.client('ec2', region_name='us-east-1').describe_regions()['Regions']] | |
def get_ec2_instances(self, region, queue): | |
instances = {} | |
response = self.session.client('ec2', region_name=region).describe_instances(Filters=[{'Name': 'instance-state-code','Values': ['16',]},]) | |
if response['Reservations']: | |
for reservations in response['Reservations']: | |
if reservations['Instances']: | |
for instance in reservations['Instances']: | |
instance_name = '' | |
if 'Tags' in instance: | |
for name in instance['Tags']: | |
if name['Key'] == 'Name': | |
instance_name = name['Value'] | |
instance_ip = '' | |
instance_eip = '' | |
if instance['State']['Name'] == ('running' or 'stopping' or 'shutting-down'): | |
if instance['PrivateIpAddress']: | |
instance_ip = instance['PrivateIpAddress'] | |
if instance['PublicIpAddress']: | |
instance_eip = instance['PublicIpAddress'] | |
data = {'name': instance_name, 'id': instance['InstanceId'], 'type': instance['InstanceType'], | |
'state': instance['State']['Name'], 'internal_ip': instance_ip, | |
'external_ip': instance_eip} | |
if region in instances: | |
instances[region].append(data) | |
else: | |
instances[region] = [data] | |
queue.put(instances) | |
queue.task_done() | |
if __name__ == '__main__': | |
try: | |
profile = sys.argv[1] | |
except IndexError: | |
profile = 'default' | |
ssh_user = sys.argv[2] | |
ssh_key = sys.argv[3] | |
aws = Aws('us-east-1', profile) | |
regions_list = aws.get_regions() | |
q = Queue() | |
threads = [] | |
for r in regions_list: | |
t = Thread(target=aws.get_ec2_instances, args=(r, q), daemon=True) | |
t.start() | |
threads.append(t) | |
for t in threads: | |
t.join() | |
q.join() | |
results = {} | |
while q.qsize(): | |
q_item = q.get() | |
if q_item: | |
results = {**results, **q_item} | |
i = 1 | |
instancess = {} | |
instances_ips = [''] | |
for k, v in results.items(): | |
print(f'region {k}') | |
instancess[k] = [] | |
for instance in v: | |
print(f"{i}. instance: {instance['id']}, {instance['name']}, {instance['external_ip']}") | |
i += 1 | |
instances_ips.append(instance['external_ip']) | |
instancess[k].append([i, instance['id'], instance['name'], instance['external_ip']]) | |
var = int(input("Connect to instance: ")) | |
print(f'connecting {var}, ip {instances_ips[var]}') | |
os.system(f'ssh {ssh_user}@{instances_ips[var]} -i {ssh_key}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment