我开始疯狂使用“JQ”解析来自 AWS CLI 的 json 输出。
也许我还不明白如何正确使用 JQ,但我喜欢解析 2 个(或更多)安全组,每个规则都有一个字符串格式,包括入站和出站
AWS CLI 命令与此有关:
aws ec2 describe-security-groups --group-ids sg-0000001 sg-0000002
输出 json 是关于这个的(我随机编辑了一些隐私信息):
{
"SecurityGroups": [
{
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"UserIdGroupPairs": []
}
],
"Description": "server-db",
"Tags": [
{
"Value": "server-db",
"Key": "Client"
},
{
"Value": "server-db",
"Key": "Name"
}
],
"IpPermissions": [
{
"ToPort": 3389,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "10.12.0.0/16"
},
{
"CidrIp": "192.168.10.10/32"
}
],
"UserIdGroupPairs": [],
"FromPort": 3389
},
{
"ToPort": 5666,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "192.168.10.10/32"
}
],
"UserIdGroupPairs": [],
"FromPort": 5666
},
{
"IpProtocol": "-1",
"IpRanges": [],
"UserIdGroupPairs": [
{
"UserId": "121211212121",
"GroupId": "sg-00000001"
}
]
},
{
"IpProtocol": "-1",
"IpRanges": [],
"UserIdGroupPairs": [
{
"UserId": "121211212121",
"GroupId": "sg-000000001"
}
]
},
{
"ToPort": -1,
"IpProtocol": "icmp",
"IpRanges": [
{
"CidrIp": "10.12.0.0/16"
},
{
"CidrIp": "192.168.10.10/32"
}
],
"UserIdGroupPairs": [],
"FromPort": -1
}
],
"GroupName": "server-db",
"VpcId": "vpc-0000001",
"OwnerId": "121211212121",
"GroupId": "sg-000000001"
},
{
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"UserIdGroupPairs": []
}
],
"Description": "server-as",
"Tags": [
{
"Value": "server-as",
"Key": "Name"
},
{
"Value": "server",
"Key": "Client"
}
],
"IpPermissions": [
{
"IpProtocol": "-1",
"IpRanges": [],
"UserIdGroupPairs": [
{
"UserId": "121211212121",
"GroupId": "sg-00000001"
}
]
},
{
"ToPort": 22,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "10.12.0.0/16"
},
{
"CidrIp": "192.168.10.10/32"
}
],
"UserIdGroupPairs": [],
"FromPort": 22
},
{
"ToPort": 443,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "10.12.0.0/16"
},
{
"CidrIp": "192.168.60.10/32"
},
{
"CidrIp": "192.168.160.10/32"
},
{
"CidrIp": "192.168.130.10/32"
},
{
"CidrIp": "192.168.130.50/32"
},
{
"CidrIp": "192.168.130.150/32"
},
{
"CidrIp": "192.168.10.10/32"
},
{
"CidrIp": "192.168.80.150/32"
},
{
"CidrIp": "192.168.80.152/32"
},
{
"CidrIp": "192.168.80.155/32"
},
{
"CidrIp": "192.168.80.158/32"
}
],
"UserIdGroupPairs": [],
"FromPort": 443
},
{
"IpProtocol": "-1",
"IpRanges": [],
"UserIdGroupPairs": [
{
"UserId": "121211212121",
"GroupId": "sg-00000002"
}
]
},
{
"ToPort": -1,
"IpProtocol": "icmp",
"IpRanges": [
{
"CidrIp": "10.12.0.0/16"
},
{
"CidrIp": "192.168.10.10/32"
}
],
"UserIdGroupPairs": [],
"FromPort": -1
}
],
"GroupName": "server-as",
"VpcId": "vpc-00000001",
"OwnerId": "121211212121",
"GroupId": "sg-00000001"
}
]
}
id 喜欢的格式可能是:
SecurityGroupId - GroupName - InBound/OutBound - IpProtocol - Port - SourceRanges/DestinationRanges
任何人都可以帮助我吗?谢谢