需要根据 Splunk SPL 中的条件从 json 中获取值
Need to get the values from json based on conditions in Splunk SPL
我将这三个条目放在一个数组中,我只想获取 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
命令来提取您在活动中拥有的 JSON 的 3 个块。然后我使用 mvexpand
为每个可能的 JSON 条目创建单独的事件。使用 Splunk spath
命令,我可以提取各个 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
表达式中排在最后。这可能会满足您的需求,或者您可以修改正则表达式以在其他情况下工作,具体取决于您的活动情况。
我将这三个条目放在一个数组中,我只想获取 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
命令来提取您在活动中拥有的 JSON 的 3 个块。然后我使用 mvexpand
为每个可能的 JSON 条目创建单独的事件。使用 Splunk spath
命令,我可以提取各个 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
表达式中排在最后。这可能会满足您的需求,或者您可以修改正则表达式以在其他情况下工作,具体取决于您的活动情况。