我正在尝试从具有VPC对等连接(pcx)的所有现有路由表中打印AWS路由表的特定字段
#!/bin/bash
export PS4='${LINENO}:'
export AWS_REGION=us-east-1
echo "AccountID," "RouteTableName," "RouteTableId," "SubnetName," "SubnetId,"
"Target," "SubnetCidrBlock," "DestinationCidrBlock," "Status," >>nonprod.csv
#set -x
for tables in nonprod
do
aws ec2 describe-route-tables --region us-east-1 > RoutetablesOld.txt
aws ec2 describe-subnets --region us-east-1 > SubnetsOld.txt
declare -i numroutetable
declare -a numsubnets
numsubnets=$(jq ".Subnets|length" SubnetsOld.txt)
numroutetable=$(jq -r '.RouteTables|length' RoutetablesOld.txt)
for ((i=0;i<$numroutetable;i++))
do
declare -i numroutes
numroutes=$(jq ".RouteTables[$i].Routes|length" RoutetablesOld.txt)
for ((k=0;k<$numroutes;k++))
do
declare -i numassociations
numassociations=$(jq ".RouteTables[$i].Associations|length"
RoutetablesOld.txt)
for ((j=0;j<$numassociations;j++))
do
for ((a=0;a<$numsubnets;a++))
do
SubnetId=$(jq -r ".RouteTables[$i].Associations[$j].SubnetId"
RoutetablesOld.txt)
RouteTableId=$(jq -r
".RouteTables[$i].Associations[$j].RouteTableId"
RoutetablesOld.txt)
RouteTableName=$(jq -r ".RouteTables[$i] |(.Tags[] |
select(.Key==\"Name\") | .Value)" RoutetablesOld.txt)
DestinationCidrBlock=$(jq -r
".RouteTables[$i].Routes[$k].DestinationCidrBlock"
RoutetablesOld.txt)
VpcPeeringConnectionId=$(jq -r
".RouteTables[$i].Routes[$k].VpcPeeringConnectionId" RoutetablesOld.txt)
TransitGatewayId=$(jq -r
".RouteTables[$i].Routes[$k].TransitGatewayId" RoutetablesOld.txt)
Subnet=$(jq -r ".Subnets[$a].SubnetId" SubnetsOld.txt)
SubnetCidrBlock=$(jq -r ".Subnets[$a].CidrBlock"
SubnetsOld.txt)
SubnetName=$(jq -r ".Subnets[$a] |(.Tags[] |
select(.Key==\"Name\") | .Value)" SubnetsOld.txt)
if [ "${VpcPeeringConnectionId}" != "null" ] && [
"${SubnetId}" == "${Subnet}" ]; then
echo "\"Nonprod\"," "\"$RouteTableName\","
"\"$RouteTableId\"," "\"$SubnetName\"," "\"$SubnetId\","
"\"$VpcPeeringConnectionId\"," "\"$SubnetCidrBlock\","
"\"$DestinationCidrBlock\"," "Pre-Rollout">>nonprod.csv
elif [ "${TransitGatewayId}" != "null" ] && [ "${SubnetId}"
== "${Subnet}" ]; then
echo "\"Nonprod\"," "\"$RouteTableName\","
"\"$RouteTableId\"," "\"$SubnetName\"," "\"$SubnetId\","
"\"$TransitGatewayId\"," "\"$SubnetCidrBlock\","
"\"$DestinationCidrBlock\"," "Pass">>nonprod.csv
else
echo "Target cidrs are neither pcx nor tgw"
fi
continue
done
done
done
done
done
我生成的输出文件将包含以下字段
AccountID,RouteTableName,RouteTableId,SubnetName,SubnetId,Target,SubnetCidrBlock,DestinationCidrBlock,状态
我正在尝试从现有路由表中打印所有pcx路由,并使用子网cidr块打印相关的子网。enter code here
但是,我的输出仅打印来自两个路由表的pcx,并且脚本继续运行。输出文件仅具有来自两个路由表的条目。仅一个子网。