如何用Kusto(KQL)在两列中查找匹配CIDR的IP?技术求助
问题详情
数据集

需求
- 从
SrcIP_s和DestIP_s两列中,筛选出与动态CIDR列表匹配的IP(可使用ipv4_is_in_range()函数)
当前实现问题
用mv-apply写的代码只能筛选出两列同时匹配CIDR的行,不符合“任一匹配”的要求,代码如下:
let customer_cidrs = dynamic(['172.16.1.128/27']); AzureNetworkAnalytics_CL | mv-apply SrcCIDR = customer_cidrs to typeof(string) on (where ipv4_is_in_range(SrcIP_s, SrcCIDR)) | mv-apply DestCIDR = customer_cidrs to typeof(string) on (where ipv4_is_in_range(DestIP_s, DestCIDR)) | project FlowDirection_s, SrcIP_s, DestIP_s, InMiBytes=round((InboundBytes_d/1048576),2), OutMiBytes=round((OutboundBytes_d/1048576),2)
限制条件
- 必须保留
*MiBytes列数据(后续要用来汇总统计各匹配CIDR的流量)
预期结果
筛选出SrcIP_s或DestIP_s任一匹配CIDR列表的行,不修改其他列值,排除无匹配的行
解决方案
你两次使用mv-apply相当于叠加了“且”的过滤逻辑,自然会丢掉仅单IP匹配的行。这里提供两种符合需求的写法:
写法一:用exists实现“或”逻辑
let customer_cidrs = dynamic(['172.16.1.128/27']); AzureNetworkAnalytics_CL | where exists ( select * from customer_cidrs where ipv4_is_in_range(SrcIP_s, customer_cidrs) ) or exists ( select * from customer_cidrs where ipv4_is_in_range(DestIP_s, customer_cidrs) ) | project FlowDirection_s, SrcIP_s, DestIP_s, InMiBytes=round((InboundBytes_d/1048576),2), OutMiBytes=round((OutboundBytes_d/1048576),2)
写法二:用mv-expand+summarize去重
let customer_cidrs = dynamic(['172.16.1.128/27']); AzureNetworkAnalytics_CL | extend cidr_list = customer_cidrs | mv-expand cidr_list to typeof(string) | where ipv4_is_in_range(SrcIP_s, cidr_list) or ipv4_is_in_range(DestIP_s, cidr_list) | summarize by FlowDirection_s, SrcIP_s, DestIP_s, InMiBytes=round((InboundBytes_d/1048576),2), OutMiBytes=round((OutboundBytes_d/1048576),2)
说明
- 写法一直接检查每行的SrcIP或DestIP是否在CIDR列表中,保留原始行结构,无需去重
- 写法二先展开CIDR列表,筛选匹配行后用
summarize by去重,避免同一行匹配多个CIDR时重复输出
内容的提问来源于stack exchange,提问作者falken
相关产品推荐
相关产品推荐

