feat: ported all scripts from bash to python. results are now written to a spreadsheet
This commit is contained in:
+21
-4
@@ -8,26 +8,43 @@ License: This program is released under the MIT License
|
||||
# Imports
|
||||
import boto3
|
||||
import concurrent.futures
|
||||
from openpyxl import load_workbook
|
||||
from openpyxl.worksheet.worksheet import Worksheet
|
||||
|
||||
def getRegions(all_regions=False):
|
||||
ec2 = boto3.client('ec2')
|
||||
response = ec2.describe_regions(AllRegions=all_regions)
|
||||
return [region['RegionName'] for region in response['Regions']]
|
||||
|
||||
def printResources(region_name: str):
|
||||
def printResources(region_name: str) -> list[list[str | int]]:
|
||||
return_data = []
|
||||
client = boto3.client('ec2', region_name=region_name)
|
||||
response = client.describe_subnets()
|
||||
for i in response['Subnets']:
|
||||
default_vpc_check = client.describe_vpcs(VpcIds=[i['VpcId']])
|
||||
for v in default_vpc_check['Vpcs']:
|
||||
print(f"{i['SubnetId']}, {i['VpcId']}, {i['CidrBlock']}, {i['AvailabilityZone']}, {v['IsDefault']}")
|
||||
return_data.append([i['SubnetId'], i['VpcId'], i['CidrBlock'], i['AvailabilityZone'], v['IsDefault']])
|
||||
return return_data
|
||||
|
||||
# Main function
|
||||
def main() -> None:
|
||||
print("SubnetId, VpcId, CidrBlock, AvailabilityZone, InDefaultVpc")
|
||||
# Open spreadsheet and add a sheet
|
||||
wb = load_workbook('aws-inventory.xlsx')
|
||||
ws = wb.create_sheet("Subnets")
|
||||
|
||||
final_data = []
|
||||
ws.append(["SubnetId", "VpcId", "CidrBlock", "AvailabilityZone", "InDefaultVpc"])
|
||||
with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor:
|
||||
futures = [executor.submit(printResources, region_name=r) for r in getRegions()]
|
||||
results = [future.result() for future in concurrent.futures.wait(futures).done]
|
||||
for future in concurrent.futures.as_completed(futures):
|
||||
region_data = future.result()
|
||||
final_data.extend(region_data)
|
||||
|
||||
clean_data = [row for row in final_data if row]
|
||||
for row in clean_data:
|
||||
ws.append(row)
|
||||
|
||||
wb.save('aws-inventory.xlsx')
|
||||
|
||||
# Call main function
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user