Skip to content

Commit

Permalink
Added details
Browse files Browse the repository at this point in the history
  • Loading branch information
asanz-mrmilu committed Oct 7, 2022
1 parent 025fc29 commit be002e0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ You will require a profile with access to ec2 listing.
Help command:

```
usage: lister.py [-h] [-r REGION] -p PROFILE [-fk [FILTER_KEY [FILTER_KEY ...]]]
[-fv [FILTER_VALUE [FILTER_VALUE ...]]] [-l]
usage: lister.py [-h] [-r REGION] -p PROFILE [-fk [FILTER_KEY [FILTER_KEY ...]]] [-fv [FILTER_VALUE [FILTER_VALUE ...]]] [-l] [-i INSTANCE_ID]
This script will list your ec2 instance with a given profile.
You may also define a region (if not configured on the profile this is required), and you can filter. A few examples:
Expand All @@ -30,8 +29,8 @@ You may also define a region (if not configured on the profile this is required)
- Complex filtering patterns!
lister.py -p leo -r us-west-2 -fk tag:env tag:role -fv staging,beta webservers
Aditionally, you can list how many instances per region you have in case you don't know which region you are searching for
after this, you can filter adding the region you found instances for
- Get details from an instance
lister.py -p leo -i i-1234567890abcdef0
- Find out how many instances per region you have
lister.py -p leo -l
Expand All @@ -47,7 +46,8 @@ optional arguments:
-fv [FILTER_VALUE [FILTER_VALUE ...]], --filter_value [FILTER_VALUE [FILTER_VALUE ...]]
Value used for filtering (one or more)
-l, --list Ammount of instances per region (one or more)
-i INSTANCE_ID, --instance-id INSTANCE_ID
Get instance details nicely formated
```


Expand Down
54 changes: 38 additions & 16 deletions lister.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import argparse
from argparse import RawTextHelpFormatter
import datetime
import json
from rich.json import JSON
from rich.console import Console
from rich.table import Table

Expand All @@ -26,10 +28,8 @@
- Complex filtering patterns!
lister.py -p leo -r us-west-2 -fk tag:env tag:role -fv staging,beta webservers
Aditionally, you can list how many instances per region you have in case you don't know which region you are searching for
after this, you can filter adding the region you found instances for
- Get details from an instance
lister.py -p leo -i i-1234567890abcdef0
- Find out how many instances per region you have
lister.py -p leo -l
Expand All @@ -40,19 +40,22 @@
parser.add_argument('-fk','--filter_key', help='Key used for filtering', required=False, default=None, nargs='*')
parser.add_argument('-fv','--filter_value', help='Value used for filtering (one or more)', required=False, default=None, nargs='*')
parser.add_argument('-l','--list', help='Ammount of instances per region (one or more)', required=False, default=None, action='store_true')
parser.add_argument('-i','--instance-id', help='Get instance details nicely formated', required=False, default=None)
args = vars(parser.parse_args())

console = Console()
if args['region'] != None:
session = boto3.session.Session(profile_name=args['profile'], region_name=args['region'])
else:
session = boto3.session.Session(profile_name=args['profile'])
ec2 = session.resource('ec2')


def lister():
def lister(ec2):
if args['list'] != None:
regions = ['us-west-1', 'us-west-2', 'us-east-1', 'us-east-2', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'eu-central-1', 'eu-north-1', 'ap-south-1', 'ap-southeast-1', 'ap-northeast-1', 'ap-northeast-2',
'ap-northeast-3', 'ap-southeast-1', 'ap-southeast-2', 'sa-east-1', 'ca-central-1']
for region in regions:
instances = 0
session = boto3.session.Session(profile_name=args['profile'], region_name=region)
ec2 = session.resource('ec2')
with console.status("[bold green]Getting instances...", spinner="dots") as status:
for instance in ec2.instances.all():
instances = instances+1
Expand All @@ -61,12 +64,29 @@ def lister():
else:
console.log("Found [bold underline red on black]{}[/] instances on region [bold underline white on black]{}[/]".format(instances,region), style="bold red" )

def main():
if args['region'] != None:
session = boto3.session.Session(profile_name=args['profile'], region_name=args['region'])
else:
session = boto3.session.Session(profile_name=args['profile'])

def instance(ec2):
with console.status("[bold green]Getting instances...", spinner="dots"):
instance = ec2.Instance(args['instance_details'])
table = Table(show_header=True, header_style="bold magenta", show_lines=True)
table.add_column("Attribute", style="white bold dim", width=20)
table.add_column("Value", style="white dim")
table.add_row("Instance ID", instance.id)
table.add_row("Instance Type", instance.instance_type)
table.add_row("Instance State", instance.state['Name'])
table.add_row("Instance Launch Time", str(instance.launch_time))
table.add_row("Instance Public IP", instance.public_ip_address)
table.add_row("Instance Private IP", instance.private_ip_address)
table.add_row("Instance Public DNS", instance.public_dns_name)
table.add_row("Instance Private DNS", instance.private_dns_name)
table.add_row("Instance Key Name", instance.key_name)
table.add_row("Instance IAM Role", instance.iam_instance_profile)
table.add_row("Instance VPC ID", instance.vpc_id)
table.add_row("Instance Subnet ID", instance.subnet_id)
table.add_row("Instance Security Groups", JSON(json.dumps(instance.security_groups)))
table.add_row("Instance Tags", JSON(json.dumps(instance.tags)))
console.print(table)

def main(ec2):
if args['filter_key'] and args['filter_value'] != None:
filter = [{'Name': 'instance-state-name', 'Values': ['running']}]
# allow multiple sets of filter keys and values
Expand Down Expand Up @@ -111,6 +131,8 @@ def main():
console.print(ec2_table)

if args['list'] != None:
lister()
lister(ec2)
elif args['instance_id'] != None:
instance(ec2)
else:
main()
main(ec2)

0 comments on commit be002e0

Please sign in to comment.