Skip to main content
Version: v1.12.0

cidr_match

Function

cidr_match test if IP is in a network

Synopsis

cidr_match(network: net, val: any) -> bool

Description

The cidr_match function returns true if val contains an IP address that falls within the network given by network. When val is a complex type, the function traverses its nested structure to find any ip values. If network is not type net, then an error is returned.

Examples

Test whether values are IP addresses in a network:

echo '10.1.2.129 11.1.2.129 10 "foo"' | zq -z 'yield cidr_match(10.0.0.0/8, this)' -

=>

true
false
false
false

It also works for IPs in complex values:

echo '[10.1.2.129,11.1.2.129] {a:10.0.0.1} {a:11.0.0.1}' | zq -z 'yield cidr_match(10.0.0.0/8, this)' -

=>

true
true
false

The first argument must be a network:

echo '10.0.0.1' | zq -z 'yield cidr_match([1,2,3], this)' -

=>

error({message:"cidr_match: not a net",on:[1,2,3]})