简体   繁体   English

解析 AWS CLI 命令

[英]Parsing AWS CLI commands

I want to parse the output of AWS CLI command我想解析 AWS CLI 命令的 output

aws ec2 describe-transit-gateways

在此处输入图像描述

在此处输入图像描述

From the above, the output I want to get is the name of the transit gateway which is given as a tag example: ("Name": "dev-tgw")从上面,我想得到的 output 是作为标签示例给出的中转网关的("Name": "dev-tgw")

I am able to retrieve TGW ID and Transit Gateway Owner from the below script我可以从下面的脚本中检索 TGW ID 和 Transit Gateway Owner

result=`aws ec2 describe-transit-gateways --profile $acc`

  for tgw in $(echo "${result}" |jq -r '.TransitGateways[] | @base64'); do

    _jq() {

      echo ${tgw} | base64 --decode | jq -r ${1}

    }
    
    Tgw=$(_jq '.TransitGatewayId')
    Tgw_owner=$(_jq '.OwnerId')
    Tgw_name=$(_jq '.Tags.Name')

    echo "$acc.Name"
    echo "Transit_Gateway": "$Tgw"
    echo "TGW_Owner_ID": "$Tgw_owner"
    echo "TGW_name": "$Tgw_name"

But I am not able to retrieve the Name of the TGW using但我无法使用检索 TGW 的名称

 Tgw_name=$(_jq '.Tags.Name')
 echo "TGW_name": "$Tgw_name"

What am I missing or am I looping it wrong?我错过了什么或者我循环错了?

You can try the following version:您可以尝试以下版本:

result=$(aws ec2 describe-transit-gateways --profile $acc)

for tgw in $(echo "${result}" | jq -r '.TransitGateways[] | @base64'); do

    _jq() {
      echo ${tgw} | base64 --decode | jq -r "${1}"
    }
    
    Tgw=$(_jq '.TransitGatewayId')
    Tgw_owner=$(_jq '.OwnerId')
    Tgw_name=$(_jq '.Tags[] | select(.Key == "Name").Value')
    
    echo "$acc.Name"
    echo "Transit_Gateway": "$Tgw"
    echo "TGW_Owner_ID": "$Tgw_owner"
    echo "TGW_name": "$Tgw_name"
    
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM