2017-10-05 76 views
0

基本上我試圖遍歷我的AWS安全組,找到使用0.0.0.0/0的任何CIDR。如何迭代JQ中的CIDR塊?

這是我的例子JSON文件:

{ 
    "SecurityGroups": [ 
     { 
      "IpPermissionsEgress": [], 
      "Description": "AWS OpsWorks load balancer - do not change or delete", 
      "IpPermissions": [ 
       { 
        "PrefixListIds": [], 
        "FromPort": 22, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 22, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 80, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 80, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 443, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 443, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       } 
      ], 
      "GroupName": "AWS-OpsWorks-LB-Server", 
      "OwnerId": "056146032236", 
      "GroupId": "sg-7dd13739" 
     }, 
     { 
      "IpPermissionsEgress": [ 
       { 
        "IpProtocol": "-1", 
        "PrefixListIds": [], 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       } 
      ], 
      "Description": "SG for bastion hosts", 
      "Tags": [ 
       { 
        "Value": "bastion-host-sg", 
        "Key": "Name" 
       } 
      ], 
      "IpPermissions": [ 
       { 
        "PrefixListIds": [], 
        "FromPort": 80, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 80, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 1991, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 1991, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 8080, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 8080, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 1194, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 1194, 
        "IpProtocol": "udp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 22, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 22, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [ 
         { 
          "CidrIpv6": "::/0" 
         } 
        ] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 30, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": -1, 
        "IpProtocol": "icmp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 1194, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 1194, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 53, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 53, 
        "IpProtocol": "udp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 53, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 53, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 443, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": 443, 
        "IpProtocol": "tcp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       }, 
       { 
        "PrefixListIds": [], 
        "FromPort": 8, 
        "IpRanges": [ 
         { 
          "CidrIp": "0.0.0.0/0" 
         } 
        ], 
        "ToPort": -1, 
        "IpProtocol": "icmp", 
        "UserIdGroupPairs": [], 
        "Ipv6Ranges": [] 
       } 
      ], 
      "GroupName": "bastion-host-sg", 
      "VpcId": "vpc-effd0e8a", 
      "OwnerId": "056146032236", 
      "GroupId": "sg-0f60196a" 
     } 
     ] 
} 

這裏是我試圖運行命令,但得到一個錯誤:

$ cat sg-small.json | jq '.SecurityGroups[].IpPermissions[].IpRanges[] | map(select(any(.CidrIp == "0.0.0.0/0")))'  
jq: error (at <stdin>:227): Cannot iterate over string ("0.0.0.0/0") 

任何想法,爲什麼發生這種情況。該方法似乎適用於其他非IP地址的事物。

回答

0

你的表達:

.SecurityGroups[].IpPermissions[].IpRanges[] 

只是CidrIp對象的流,所以這不是你想要的。

下面將選擇符合標準的SecurityGroups:

.SecurityGroups[] 
| select(any(.IpPermissions[].IpRanges[]; .CidrIp == "0.0.0.0/0")) 

這是否是你想要正是目前尚不清楚,因爲你並沒有指明。請參閱http://stackoverflow.com/help/mcve

0

的原因,你看到的錯誤

Cannot iterate over string ("0.0.0.0/0") 

是因爲你的數據的過濾器的第一部分

.SecurityGroups[].IpPermissions[].IpRanges[] 

生成的對象序列

{ 
    "CidrIp": "0.0.0.0/0" 
} 
... 

您的篩選器的部分map terates了在這些對象的值,通過

"0.0.0.0/0" 

來表達select(any(.CidrIp == "0.0.0.0/0"))其中any試圖迭代字符串中的所有值和失敗,你觀察到的錯誤。

如果你只想看到{"CidrIp":...}對象的序列上面可以消除mapany

.SecurityGroups[].IpPermissions[].IpRanges[] 
| select(.CidrIp == "0.0.0.0/0") 

如果你寧願收集這些對象到一個數組,你可以刪除any和移動一些例如,重複進入map

.SecurityGroups 
| map(.IpPermissions[].IpRanges[] | select(.CidrIp == "0.0.0.0/0")) 

生產

[ 
    { 
    "CidrIp": "0.0.0.0/0" 
    }, 
    .... 

結果數組的顯式建築更容易看到,如果你與它的定義替換map。由於map(f)被定義爲[ .[] | f ]以上過濾器與此相同:

.SecurityGroups 
| [ .[] | .IpPermissions[].IpRanges[] | select(.CidrIp == "0.0.0.0/0") ]