Created
December 13, 2024 06:46
-
-
Save brunodasilvalenga/efb6aff8ddc6c179c867088879ee5ab0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set output file | |
report_file="aws_network_report_$(date +%Y%m%d_%H%M%S).txt" | |
echo "AWS Network Infrastructure Report" > "$report_file" | |
echo "Generated on: $(date)" >> "$report_file" | |
echo "========================================" >> "$report_file" | |
# First, let's get Transit Gateways information | |
echo -e "\nTransit Gateways:" >> "$report_file" | |
echo "----------------------------------------" >> "$report_file" | |
aws ec2 describe-transit-gateways \ | |
--query 'TransitGateways[*].[TransitGatewayId,OwnerId,Tags[?Key==`Name`].Value|[0]]' \ | |
--output text | while IFS=$'\t' read -r tgw_id owner_id tgw_name; do | |
tgw_name=${tgw_name:-"<No Name>"} | |
echo "Transit Gateway:" >> "$report_file" | |
echo " Name: $tgw_name" >> "$report_file" | |
echo " ID: $tgw_id" >> "$report_file" | |
echo " Owner: $owner_id" >> "$report_file" | |
echo "" >> "$report_file" | |
done | |
# Get list of VPCs | |
vpcs=$(aws ec2 describe-vpcs --query 'Vpcs[*].[VpcId,CidrBlock,Tags[?Key==`Name`].Value | [0]]' --output text) | |
while IFS=$'\t' read -r vpc_id cidr vpc_name; do | |
vpc_name=${vpc_name:-"<No Name>"} | |
echo "VPC Details:" >> "$report_file" | |
echo "========================================" >> "$report_file" | |
echo "Name: $vpc_name" >> "$report_file" | |
echo "ID: $vpc_id" >> "$report_file" | |
echo "CIDR: $cidr" >> "$report_file" | |
echo -e "\nSubnets:" >> "$report_file" | |
echo "----------------------------------------" >> "$report_file" | |
# Get subnets for this VPC | |
aws ec2 describe-subnets \ | |
--filters "Name=vpc-id,Values=$vpc_id" \ | |
--query 'Subnets[*].[SubnetId,CidrBlock,Tags[?Key==`Name`].Value | [0]]' \ | |
--output text | while IFS=$'\t' read -r subnet_id subnet_cidr subnet_name; do | |
subnet_name=${subnet_name:-"<No Name>"} | |
echo " Subnet:" >> "$report_file" | |
echo " Name: $subnet_name" >> "$report_file" | |
echo " ID: $subnet_id" >> "$report_file" | |
echo " CIDR: $subnet_cidr" >> "$report_file" | |
echo "" >> "$report_file" | |
done | |
echo "Route Tables:" >> "$report_file" | |
echo "----------------------------------------" >> "$report_file" | |
# Get route tables for this VPC | |
aws ec2 describe-route-tables \ | |
--filters "Name=vpc-id,Values=$vpc_id" \ | |
--output json | jq -r '.RouteTables[] | @base64' | while read -r rt_encoded; do | |
rt_json=$(echo "$rt_encoded" | base64 --decode) | |
# Extract route table basic info | |
rt_id=$(echo "$rt_json" | jq -r '.RouteTableId') | |
rt_name=$(echo "$rt_json" | jq -r '.Tags[]? | select(.Key=="Name") | .Value // "<No Name>"') | |
echo " Route Table:" >> "$report_file" | |
echo " Name: $rt_name" >> "$report_file" | |
echo " ID: $rt_id" >> "$report_file" | |
# Process subnet associations | |
echo " Associated Subnets:" >> "$report_file" | |
subnet_associations=$(echo "$rt_json" | jq -r '.Associations[].SubnetId // empty') | |
if [ -n "$subnet_associations" ]; then | |
while read -r subnet; do | |
if [ -n "$subnet" ]; then | |
subnet_name=$(aws ec2 describe-subnets \ | |
--subnet-ids "$subnet" \ | |
--query 'Subnets[0].Tags[?Key==`Name`].Value | [0]' \ | |
--output text) | |
subnet_name=${subnet_name:-"<No Name>"} | |
echo " - $subnet_name ($subnet)" >> "$report_file" | |
fi | |
done <<< "$subnet_associations" | |
else | |
echo " None (Main Route Table)" >> "$report_file" | |
fi | |
# Process routes | |
echo " Routes:" >> "$report_file" | |
echo "$rt_json" | jq -r '.Routes[]? | "\(.DestinationCidrBlock // .DestinationPrefixListId // .DestinationIpv6CidrBlock) -> \(.GatewayId // .NatGatewayId // .TransitGatewayId // .NetworkInterfaceId // .VpcPeeringConnectionId // "local" | select(. != "null"))"' | while read -r route; do | |
echo " - $route" >> "$report_file" | |
done | |
echo "" >> "$report_file" | |
done | |
echo "NAT Gateways:" >> "$report_file" | |
echo "----------------------------------------" >> "$report_file" | |
# Get NAT Gateways for this VPC | |
aws ec2 describe-nat-gateways \ | |
--filter "Name=vpc-id,Values=$vpc_id" \ | |
--query 'NatGateways[*].[NatGatewayId,SubnetId,ConnectivityType,PrivateIp,Tags[?Key==`Name`].Value|[0]]' \ | |
--output text | while IFS=$'\t' read -r nat_id subnet_id connectivity_type private_ip nat_name; do | |
nat_name=${nat_name:-"<No Name>"} | |
echo " NAT Gateway:" >> "$report_file" | |
echo " Name: $nat_name" >> "$report_file" | |
echo " ID: $nat_id" >> "$report_file" | |
echo " Connectivity Type: $connectivity_type" >> "$report_file" | |
echo " Private IPv4: $private_ip" >> "$report_file" | |
# Get subnet name | |
subnet_name=$(aws ec2 describe-subnets \ | |
--subnet-ids "$subnet_id" \ | |
--query 'Subnets[0].Tags[?Key==`Name`].Value | [0]' \ | |
--output text) | |
subnet_name=${subnet_name:-"<No Name>"} | |
echo " Attached Subnet: $subnet_name ($subnet_id)" >> "$report_file" | |
echo "" >> "$report_file" | |
done | |
echo "========================================" >> "$report_file" | |
echo "" >> "$report_file" | |
done <<< "$vpcs" | |
echo "Report generated: $report_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment