forked from paulbayer/Inventory_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_my_vpcs.py
More file actions
executable file
·148 lines (134 loc) · 5.21 KB
/
all_my_vpcs.py
File metadata and controls
executable file
·148 lines (134 loc) · 5.21 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os, sys, pprint
import Inventory_Modules
import argparse
from colorama import init,Fore,Back,Style
from botocore.exceptions import ClientError, NoCredentialsError
import logging
init()
parser = argparse.ArgumentParser(
description="We\'re going to find all vpcs within any of the profiles we have access to.",
prefix_chars='-+/')
parser.add_argument(
"-p","--profile",
dest="pProfiles",
nargs="*",
metavar="profile to use",
default=["default"],
help="To specify a specific profile, use this parameter. Default will be ALL profiles, including those in ~/.aws/credentials and ~/.aws/config")
parser.add_argument(
"-r","--region",
nargs="*",
dest="pregion",
metavar="region name string",
default=["us-east-1"],
help="String fragment of the region(s) you want to check for resources.")
parser.add_argument(
"--default",
dest="pDefaultOnly",
metavar="Default Only flag",
action="store_const",
const=True, # args.loglevel = 10
default=False, # args.loglevel = 50
help="Flag to determine whether default VPCs are included in the output.")
parser.add_argument(
'-d', '--debug',
help="Print LOTS of debugging statements",
action="store_const",
dest="loglevel",
const=logging.DEBUG, # args.loglevel = 10
default=logging.CRITICAL) # args.loglevel = 50
parser.add_argument(
'-vvv',
help="Print INFO level statements",
action="store_const",
dest="loglevel",
const=logging.INFO, # args.loglevel = 20
default=logging.CRITICAL) # args.loglevel = 50
parser.add_argument(
'-vv', '--verbose',
help="Be MORE verbose",
action="store_const",
dest="loglevel",
const=logging.WARNING, # args.loglevel = 30
default=logging.CRITICAL) # args.loglevel = 50
parser.add_argument(
'-v',
help="Be verbose",
action="store_const",
dest="loglevel",
const=logging.ERROR, # args.loglevel = 40
default=logging.CRITICAL) # args.loglevel = 50
args = parser.parse_args()
pProfiles=args.pProfiles
pRegionList=args.pregion
pDefaultOnly=args.pDefaultOnly
verbose=args.loglevel
logging.basicConfig(level=args.loglevel, format="[%(filename)s:%(lineno)s:%(levelname)s - %(funcName)30s() ] %(message)s")
SkipProfiles=["default","Shared-Fid"]
##########################
ERASE_LINE = '\x1b[2K'
NumVpcsFound = 0
NumRegions = 0
print()
fmt='%-20s %-10s %-21s %-20s %-12s %-10s'
print(fmt % ("Profile","Region","Vpc ID","CIDR","Is Default?","Vpc Name"))
print(fmt % ("-------","------","------","----","-----------","--------"))
RegionList=Inventory_Modules.get_ec2_regions(pRegionList)
ProfileList=Inventory_Modules.get_profiles(SkipProfiles,pProfiles)
logging.info("# of Regions: %s" % len(RegionList))
logging.info("# of Profiles: %s" % len(ProfileList))
for region in RegionList:
NumRegions += 1
NumProfilesInvestigated = 0 # I only care about the last run - so I don't get profiles * regions.
for profile in ProfileList:
NumProfilesInvestigated += 1
try:
Vpcs=Inventory_Modules.find_profile_vpcs(profile,region,pDefaultOnly)
logging.info("Info - Profile %s | Region %s | Found %s vpcs",profile,region,len(Vpcs))
VpcNum=len(Vpcs['Vpcs']) if 'Vpcs' in Vpcs else 0
print(ERASE_LINE,"Profile: {} | Region: {} | Found {} Vpcs".format(profile,region,VpcNum),end='\r')
# print("Profile: {} | Region: {} | Found {} Vpcs".format(profile,region,VpcNum))
except ClientError as my_Error:
if str(my_Error).find("AuthFailure") > 0:
print("{}: Authorization Failure connecting to {}".format(profile,region))
pass
except TypeError as my_Error:
print(my_Error)
logging.info("There was an error")
pass
# if 'Vpcs' in Vpcs and len(Vpcs['Vpcs']) > 0:
if 'Vpcs' in Vpcs: # If there are no VPCs, you can't reference the index
logging.info("Displaying profile %s",profile)
for y in range(len(Vpcs['Vpcs'])):
VpcId=Vpcs['Vpcs'][y]['VpcId']
IsDefault=Vpcs['Vpcs'][y]['IsDefault']
CIDR=Vpcs['Vpcs'][y]['CidrBlock']
if 'Tags' in Vpcs['Vpcs'][y]:
logging.debug("Looking for tags")
for z in range(len(Vpcs['Vpcs'][y]['Tags'])):
if Vpcs['Vpcs'][y]['Tags'][z]['Key']=="Name":
VpcName=Vpcs['Vpcs'][y]['Tags'][z]['Value']
else:
VpcName="No name defined"
print(fmt % (profile,region,VpcId,CIDR,IsDefault,VpcName))
NumVpcsFound += 1
else:
continue
# print(ERASE_LINE)
print("Found",NumVpcsFound,"Vpcs across",NumProfilesInvestigated,"profiles across",NumRegions,"regions")
print()