Untitled
unknown
plain_text
15 days ago
1.5 kB
2
Indexable
Never
#!/bin/bash SUBNET_ID=subnet-12345678 # Get the subnet CIDR CIDR_BLOCK=$(aws ec2 describe-subnets --subnet-ids $SUBNET_ID --query "Subnets[0].CidrBlock" --output text) # Calculate the total number of IPs in the CIDR block TOTAL_IPS=$(ipcalc -n $CIDR_BLOCK | grep Hosts | awk '{print $2}') # Get the network interfaces (ENIs) in the subnet ENIS=$(aws ec2 describe-network-interfaces --filters "Name=subnet-id,Values=$SUBNET_ID" --query "NetworkInterfaces[*].[NetworkInterfaceId,PrivateIpAddresses[*].PrivateIpAddress]" --output json) # Initialize counters USED_IPS=0 echo "Used IPs and their associated resources in subnet $SUBNET_ID:" # Loop through each ENI to get information about the associated resource echo "$ENIS" | jq -c '.[]' | while read eni; do ENI_ID=$(echo $eni | jq -r '.[0]') IP_ADDRESSES=$(echo $eni | jq -r '.[1][]') ((USED_IPS+= $(echo "$IP_ADDRESSES" | wc -l))) # Get the description and associated resource for the ENI RESOURCE_INFO=$(aws ec2 describe-network-interfaces --network-interface-ids $ENI_ID --query "NetworkInterfaces[0].Attachment.InstanceId,NetworkInterfaces[0].Description" --output text) echo "ENI: $ENI_ID" echo "IP Addresses: $IP_ADDRESSES" echo "Associated Resource: $RESOURCE_INFO" echo "-----------------------------------" done # Calculate the available IPs AVAILABLE_IPS=$(($TOTAL_IPS - $USED_IPS)) echo "Total IPs: $TOTAL_IPS" echo "Used IPs: $USED_IPS" echo "Available IPs: $AVAILABLE_IPS"
Leave a Comment