需要根据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"}

我不得不使用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表达式的最后。这可能会满足您的需求,或者您可以修改regex以在其他情况下工作,这取决于您的事件的外观。

最新更新