-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_dynamic_host_inventory.py
More file actions
116 lines (95 loc) · 3.68 KB
/
aws_dynamic_host_inventory.py
File metadata and controls
116 lines (95 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python3
# -------------------------------------------------------------------------------------------------------------------------------- #
# aws_dynamic_host_inventory.py : A python script meant to list and search instances based on AWS regions.
# Script can be made universal by adding respective credentails,regions and environment name
#
#
#
# Dependencies ( One time Installation )
# --------------------------------------
# 1. Python 3.6+
# 2. Additional python libraries.
# Install additional mandatory python packages using command.
# --> "pip3 install boto3 "
# --> "pip3 install prettytable"
#
# How to
# --------
# 1. Specify your aws_access_key_id and aws_secret_access_key for every profile
# Example : cat .aws/credentials
#
# [prod]
# aws_access_key_id = ANYPASSWORDOFMINE
# aws_secret_access_key = KUCHBHISECRETKEYOFMINE
#
# [non_prod]
# aws_access_key_id = ANOTHERPA55WORD!23HKDKD
# aws_secret_access_key = 7938303NDJDNDEJDNDJ9@@@0CNCKDDMD
#
# 2. Now edit one more line within a script "user_env_input = input('Your ENV (prod1/prod2/qa1/stage1) please :: ')"
# Chnage it as per your need, like l"user_env_input = input('Your ENV name (prod / non_prod please :: ')"
#
# 3. run the python script as "aws_dynamic_host_inventory.py".
# script will prompt for environmnet name (type the one that you mentioned within the credentials file and search string.)
#
# 4. Argumnet mode is also supported as python3 aws_dynamic_host_inventory.py < non_prod > < myhostname >
#
# 5. To enlist all the host within the specific region's account
# Run --> python3 aws_dynamic_host_inventory.py
# Provide your environment name and then leave blank when asked for search string
#
#
# DOC : 23-Sep-2019
# Updates : XX-XXX-2019
# : XX-XXX-2019
#
# Author : JacKuna
# --------------------------------------------------------------------------------------------------------------------------------- #
import boto3
from prettytable import PrettyTable
import sys
#Color
R = "\033[0;31;40m" # RED
G = "\033[0;32;40m" # GREEN
Y = "\033[0;33;40m" # Yellow
B = "\033[0;34;40m" # Blue
N = "\033[0m" # Reset
CYAN = "\033[1;36m" # CYAN
def check_argument():
global user_env_input
global search_string
global user_input
if (len(sys.argv)) > 2:
user_env_input = str(sys.argv[1])
user_input = str(sys.argv[2])
search_string = ''
else:
print()
user_env_input = input('Your ENV (prod1/prod2/qa1/stage1) pleas :: ')
user_input = input('Your Search String please :: ')
search_string = ''
def grab_bs_instnaces_info():
global user_env_input
global search_string
global user_input
session = boto3.session.Session(profile_name=user_env_input)
ec2 = session.client('ec2')
response = ec2.describe_instances(Filters=[{'Name': 'tag:Name', 'Values': ['*'+ user_input + search_string + '*']}])
tabular_table = PrettyTable()
tabular_table.field_names = ["BeanStalk App Name","Instance","IP Address"]
tabular_table.align["BeanStalk App Name"] = "l"
tabular_table.align["IP Address"] = "l"
for val in response['Reservations']:
for newval in val['Instances']:
for new_val in newval['Tags']:
if new_val['Key'] == 'Name':
tabular_table.add_row([CYAN+new_val['Value']+N, newval['InstanceId'], Y+newval['PrivateIpAddress']+N])
else:
pass
print()
print(tabular_table)
try:
check_argument()
grab_bs_instnaces_info()
except:
print(" Entered Environment", R+user_env_input.upper()+N , "can't be located, check again ! " )