amazon-web-services - 需要根据 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
。这可能满足您的需求,或者您可以修改正则表达式以在其他情况下工作,具体取决于您的事件的外观。
推荐阅读
- react-native - 搜索结果 Web 结果命令失败:gradlew.bat installDebug
- python - Python:双端队列超出索引,为什么?
- python-3.x - 我如何将函数中的参数提及到 af 字符串中?
- android - 在 android 应用程序中使用 fstatat64 系统调用
- python - 使用函数时将浮点数转换为 int 不起作用
- java - 使用 Android Websocket 监听 URL
- php - 是否可以将变量作为 POST 变量的参数传递?语法是什么?
- redirect - Nuxt i18n 从页面名称重定向到特定区域设置
- c++ - 从立体相机系统获取特定特征点的 3-D 世界坐标
- single-page-application - 如果不清除 SPA 上的 cookie,就不会发生逻辑重定向