首页 > 解决方案 > 需要根据 Splunk SPL 中的条件从 json 中获取值

问题描述

我在一个数组中有这三个条目,我想只获取那些具有 from_port = 22 的条目的 cidr_ip 。在这种情况下,这是具有 5 个 cidr_ips 的第二个条目

{"grants": [{"owner_id": "376456522198", "cidr_ip": null}, {"owner_id": "376456522198", "cidr_ip": null}], "ipRanges": "", "from_port": null, "to_port": null, "groups": "\n ", "ip_protocol": "-1"}
 {"grants": [{"owner_id": null, "cidr_ip": "52.59.64.149/32"}, {"owner_id": null, "cidr_ip": "193.26.194.92/32"}, {"owner_id": null, "cidr_ip": "182.75.203.18/32"}, {"owner_id": null, "cidr_ip": "49.207.49.169/32"}, {"owner_id": null, "cidr_ip": "1.39.182.12/32"}], "ipRanges": "\n ", "from_port": "22", "to_port": "22", "groups": "", "ip_protocol": "tcp"}
 {"grants": [{"owner_id": null, "cidr_ip": "52.59.64.149/32"}, {"owner_id": null, "cidr_ip": "182.75.203.18/32"}, {"owner_id": null, "cidr_ip": "193.26.194.92/32"}], "ipRanges": "\n ", "from_port": "3389", "to_port": "3389", "groups": "", "ip_protocol": "tcp"}

标签: amazon-web-servicessplunk

解决方案


我不得不使用rex命令来提取您在事件中拥有的 3 块 JSON。然后我使用mvexpand为每个可能的 JSON 条目制作单独的事件。使用 Splunkspath命令,我可以提取各个json字段。然后很容易过滤from_port=22然后只显示我们感兴趣的字段。

| rex max_match=10 "(?<json>{\"grants\":.*?ip_protocol\": \"[^\"]+\"})" 
| mvexpand json 
| spath input=json 
| where from_port=22
| table grants{}.cidr_ip, from_port

请注意,rex正则表达式依赖于表达式中的ip_protocol最后一个json。这可能满足您的需求,或者您可以修改正则表达式以在其他情况下工作,具体取决于您的事件的外观。


推荐阅读