首页 > 解决方案 > 如果 IP 在范围列表中,则过滤它们

问题描述

在 Kusto,我正在努力解决算法问题。我得到了 IP 的大列表(Azure Monitor),以及要列入白名单的范围列表。如果 IP 在第二个列表的范围内,我如何排除第一个列表的行?

当然我们会使用ipv4_is_in_range()andmv-apply为此,但我不知道如何。

样品项目:

let ranges_to_whitelist = "['127.0.0.1', 10.0.0.0/28']";
let big_table_of_rows = datatable (ip_range: string) ['1.2.3.4', '10.0.0.254', '172.16.1.2', '10.0.0.1'];

应该产生:

datatable (ip_range: string) ['10.0.0.1'];

谢谢!

标签: algorithmazure-data-explorerkql

解决方案


如果我正确理解了要求,您需要首先将范围数组扩展为白名单(使用mv-expandor mv-apply),然后根据以下条件应用过滤器ipv4_is_in_range()

let ranges_to_whitelist = dynamic(['127.0.0.1', '10.0.0.0/28']);
let big_table_of_rows = datatable (ip: string) ['1.2.3.4', '10.0.0.254', '172.16.1.2', '10.0.0.1'];
big_table_of_rows
| mv-apply ip_range = ranges_to_whitelist to typeof(string) on (
    where ipv4_is_in_range(ip, ip_range)
)
| project-away ip_range
ip
10.0.0.1

或者,对于相反的情况:

let ranges_to_whitelist = dynamic(['127.0.0.1', '10.0.0.0/28']);
let big_table_of_rows = datatable (ip: string) ['1.2.3.4', '10.0.0.254', '172.16.1.2', '10.0.0.1'];
big_table_of_rows
| mv-apply ip_range = ranges_to_whitelist to typeof(string) on (
    summarize countif(ipv4_is_in_range(ip, ip_range))
    | where countif_ == 0
)
| project-away countif_
ip
1.2.3.4
10.0.0.254
172.16.1.2

推荐阅读