我有一些需要使用jq解析的json。我正在尝试提取 如果任何SecurityGroups> IpPermissions> IpRanges> CidrIp匹配某个IP,则SecurityGroups> GroupId。
例如,搜索11.11.11.11应该返回sg-3jf32kj3j。多个安全组可能包含该IP。我需要返回所有的GroupId。
单独使用jq还是有可能吗?还是需要bash?
与仅使用Python之类的方法相比,我发现jq语法令人困惑。
{
"SecurityGroups": [{
"OwnerId": "111111111",
"Description": "default VPC security group",
"GroupId": "sg-1a1a1a1a1",
"VpcId": "vpc-1a1a1a1a1",
"IpPermissionsEgress": [{
"IpProtocol": "-1",
"PrefixListIds": [],
"Ipv6Ranges": [],
"UserIdGroupPairs": [],
"IpRanges": [{
"CidrIp": "0.0.0.0/0"
}]
}],
"GroupName": "default",
"IpPermissions": [{
"IpProtocol": "-1",
"PrefixListIds": [],
"Ipv6Ranges": [],
"UserIdGroupPairs": [{
"GroupId": "sg-5df45d5d5",
"UserId": "234234234"
}],
"IpRanges": []
}]
},
{
"OwnerId": "22222222222",
"Description": "EC2 Security Group",
"Tags": [{
"Key": "aws:cloudformation:logical-id",
"Value": "EC2SecurityGroup"
},
{
"Key": "aws:cloudformation:stack-id",
"Value": "arn:aws:cloudformation:us-west-2:111111111:stack/blah-prod-vpc/asdfsdf-j3j3-22j1-39fj-sadfsadf"
},
{
"Key": "Name",
"Value": "blah-production-EC2"
},
{
"Key": "Owner",
"Value": "blah@blah.com"
},
{
"Key": "aws:cloudformation:stack-name",
"Value": "prod-vpc"
},
{
"Key": "Created",
"Value": "2018-05-21T09:40:55-07:00"
}
],
"GroupId": "sg-3jf32kj3j",
"VpcId": "vpc-3kj3f2kj3",
"IpPermissionsEgress": [{
"IpProtocol": "-1",
"PrefixListIds": [],
"Ipv6Ranges": [],
"UserIdGroupPairs": [],
"IpRanges": [{
"CidrIp": "0.0.0.0/0"
}]
}],
"GroupName": "blah-prod-vpc-EC2SecurityGroup-SJHS78F78SSH",
"IpPermissions": [{
"IpProtocol": "tcp",
"ToPort": 80,
"UserIdGroupPairs": [],
"IpRanges": [{
"CidrIp": "0.0.0.0/0"
}],
"FromPort": 80,
"PrefixListIds": [],
"Ipv6Ranges": []
},
{
"IpProtocol": "icmp",
"ToPort": 0,
"UserIdGroupPairs": [],
"IpRanges": [{
"CidrIp": "11.11.11.11/32"
}],
"FromPort": 0,
"PrefixListIds": [],
"Ipv6Ranges": []
},
{
"IpProtocol": "-1",
"PrefixListIds": [],
"Ipv6Ranges": [],
"UserIdGroupPairs": [],
"IpRanges": [{
"CidrIp": "22.22.22.22/16"
},
{
"CidrIp": "33.33.33.33/32"
}
]
},
{
"IpProtocol": "tcp",
"ToPort": 22,
"UserIdGroupPairs": [],
"IpRanges": [{
"CidrIp": "0.0.0.0/0"
}],
"FromPort": 22,
"PrefixListIds": [],
"Ipv6Ranges": [{
"CidrIpv6": "::/0"
}]
},
{
"IpProtocol": "tcp",
"ToPort": 443,
"UserIdGroupPairs": [],
"IpRanges": [{
"CidrIp": "0.0.0.0/0"
}],
"FromPort": 443,
"PrefixListIds": [],
"Ipv6Ranges": []
},
{
"IpProtocol": "icmp",
"ToPort": -1,
"UserIdGroupPairs": [],
"IpRanges": [{
"CidrIp": "44.44.44.44/32"
},
{
"CidrIp": "55.55.55.55/29"
}
],
"FromPort": -1,
"PrefixListIds": [],
"Ipv6Ranges": []
}
]
}
]}
答案 0 :(得分:1)
以下过滤器满足规定的要求:
.SecurityGroups[]
| select( any(.IpPermissions[].IpRanges[];
.CidrIp | startswith("11.11.11.11/") ) )
| .GroupId
尤其是在您输入时,它会产生所需的值:
"sg-3jf32kj3j"
您可能想考虑使用以下更复杂的模式匹配:
test("^11.11.11.11($|/)")
如果要删除引号,请考虑使用jq的-r命令行选项。
这是另一种解决方案,但请注意,它具有完全不同的语义:
..
| select( .. | test("^11.11.11.11($|/)")? )
| .GroupId? // empty
答案 1 :(得分:0)
我可以为您提供替代解决方案吗?这是我最近开发的unix实用程序jtc
的样子:
bash $ cat file.json | jtc -w'<^11.11.11.11\/>R: [-5] [GroupId]'
"sg-3jf32kj3j"
bash $
它还将显示显示此IP地址的所有组。如果愿意,可以在github.com上找到该实用程序,或者使用jtc
和json
关键字在Google上对其进行搜索