forked from paulbayer/Inventory_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_my_functions.py
More file actions
executable file
·115 lines (99 loc) · 3.91 KB
/
all_my_functions.py
File metadata and controls
executable file
·115 lines (99 loc) · 3.91 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
#!/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 resources within any of the profiles we have access to.",
prefix_chars='-+/')
parser.add_argument(
"-p", "--profile",
dest="pProfiles",
nargs="*",
metavar="profile to use",
default="all",
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(
'-d', '--debug',
help="Print lots of debugging statements",
action="store_const",
dest="loglevel",
const=logging.DEBUG,
default=logging.CRITICAL)
parser.add_argument(
'-v', '--verbose',
help="Be verbose",
action="store_const",
dest="loglevel",
const=logging.INFO)
args = parser.parse_args()
pProfiles=args.pProfiles
pRegionList=args.pregion
logging.basicConfig(level=args.loglevel)
SkipProfiles=["default"]
def left(s, amount):
return s[:amount]
def right(s, amount):
return s[-amount:]
def mid(s, offset, amount):
return s[offset-1:offset+amount-1]
##########################
ERASE_LINE = '\x1b[2K'
NumInstancesFound = 0
NumRegions = 0
print()
fmt='%-20s %-10s %-40s %-12s %-35s'
print(fmt % ("Profile", "Region", "Function Name", "Runtime", "Role"))
print(fmt % ("-------", "------", "-------------", "-------", "----"))
RegionList=Inventory_Modules.get_ec2_regions(pRegionList)
ProfileList=Inventory_Modules.get_profiles(SkipProfiles, pProfiles)
# pprint.pprint(RegionList)
for pregion 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:
Functions=Inventory_Modules.find_profile_functions(profile, pregion)
FunctionNum=len(Functions['Functions'])
print(ERASE_LINE+"Profile:", profile, "Region:", pregion, "Found", FunctionNum, "functions", end='\r')
except ClientError as my_Error:
if str(my_Error).find("AuthFailure") > 0:
print(ERASE_LINE+profile+": Authorization Failure")
if len(Functions['Functions']) > 0:
for y in range(len(Functions['Functions'])):
# print("Y:",y,"Number:",len(Functions['Functions']))
# print("Function Name:",Functions['Functions'][y]['FunctionName'])
FunctionName=Functions['Functions'][y]['FunctionName']
Runtime=Functions['Functions'][y]['Runtime']
Rolet=Functions['Functions'][y]['Role']
Role=mid(Rolet,Rolet.find("/")+2,len(Rolet))
print(fmt % (profile,pregion,FunctionName,Runtime,Role))
NumInstancesFound += 1
print(ERASE_LINE)
print("Found",NumInstancesFound,"functions across",NumProfilesInvestigated,"profiles across",NumRegions,"regions")
print()