GREPCIDR(1)
π GREPCIDR(1) β the general commands manual, but make it cute π
okay bestie, sit down, hydrate, because today we're learning grepcidr from the General Commands Manual and honestly? it's giving main character energy for anyone who's ever stared at a firewall log at 3am. π¦
β¨ NAME β¨
grepcidr β filter IPv4 and IPv6 addresses matching CIDR patterns π
πΈ SYNOPSIS πΈ
grepcidr [OPTIONS] PATTERN [FILE...]
grepcidr [OPTIONS] [-e PATTERN | -f PATFILE] [FILE...]
π DESCRIPTION π
So basically grepcidr filters a whole list of IP addresses against one or more Classless Inter-Domain Routing (CIDR) specs. Think of it like grep but it actually understands networks instead of just vibing with text. π§
Just like grep, you get options to invert matching and to load patterns from a file. And it is capable of efficiently processing large numbers of IPs and networks β like, actually fast, no cap. π₯
β¨ OPTIONS β¨
-Vβ show software version π-cβ display the count of the matching lines instead of showing the lines themselves-iβ inverse match, include lines without an IP; this implies-v-sβ enforce strict alignment of the CIDR mask; the host portion must be all zero π―-vβ invert the sense of matching, output lines with IPs that don't match-xβ strict matching, only look at the start of line-eβ specify individual IP or CIDR pattern(s) right on the command line-fβ load individual IP or CIDR pattern(s) from a file π
π USAGE NOTES π
A PATTERN given on the command line can hold multiple patterns, separated by whitespace or commas. For long lists of network patterns, use -f to load a file where each line contains one pattern (can be IPv4 or IPv6, no judgement). Blank lines and comments starting with # are ignored. π
Every IPv4 pattern β whether it came from the command line or a file β may be:
a.b.c.d/xy (CIDR format)
a.b.c.d-e.f.g.h (IP range)
a.b.c.d (Single IP)
And similarly for IPv6:
a:b:c::/xyz (CIDR format)
a:b:c:: (Single IP)
Dotted-decimal IPv4 format, or any legal IPv6 format is supported (see: man inet_pton). π
IP addresses that appear anywhere on the input line will be compared and matched against the patterns. To be recognized, an IP (field) must end with terminating text. An IPv4 field terminates upon anything other than alphanumeric or dot. An IPv6 field terminates upon anything other than alphanumeric, dot, or colon. This is to prevent accidental matching of ambiguous text such as host names containing reverse DNS. (we love a careful queen β¨)
Use the -x option to do a strict parse without searching the whole line, and grepcidr will only look for the single IP at the start of the line. periodt.
π¦ EXAMPLES π¦
grepcidr -f ournetworks blacklist > abuse.log
Find customers (CIDR ranges in file) that appear in blacklist π
grepcidr 2001:db8::/32 log.1 log.2
Search for this IPv6 network inside two files
grepcidr 127.0.0.0/8 iplog
Searches for any localnet IP addresses inside the iplog file
grepcidr "192.168.0.1-192.168.10.13" iplog
Searches for IPs matching indicated range in the iplog file
script | grepcidr -vf whitelist > blacklist
Create a blacklist, with whitelisted networks removed (inverse) π
grepcidr -f list1 list2
Cross-reference two lists, outputs IPs common to both lists π
β EXIT STATUS β
As with grep: the exit status is 0 if matching IPs are found, and 1 if not found. If an error occurred the exit status is 2. (yes bestie, that's three whole moods π)
π AUTHOR π
This software and manual page was written by Jem Berkes <jem@berkes.ca> based on the first man page and DocBook format contributed by Ryan Finnie. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License, Version 2 or any later version published by the Free Software Foundation.
π WEB SITE π
http://www.pc-tools.net/unix/grepcidr/
π THANKS π
Sponsored in part by the Spamhaus Project, http://www.spamhaus.org/
Thanks to John Levine <johnl@taugh.com> for sharing his alternative implementation. I used a couple ideas from his code, such as portable 128-bit numbers and support for multiple input files. However, John's version is quite different and represents a significant fork in the project.
Thanks to Ryan Finnie <ryan@finnie.org> for his work on the Debian package. I've adopted several of his changes including the Makefile, and the DocBook format which now is the source of the man page. Thanks to Ryan for writing the first manual in DocBook format.
Many thanks to Dick Wesseling <ftu@fi.uu.nl> who suggested an improved data structure format as well as binary search, to improve grepcidr performance. π₯
okay that's the whole page bestie, you ate. π«Ά and just so you know, this little masterpiece lives in the General Commands Manual as GREPCIDR(1) β that's the version/source vibe and the date vibe rolled into one, so go forth and filter those IPs, you gorgeous nerd. π
β¨π
π¦ where this page comes from
- debian bookworm: grepcidr 2.0-2
- debian trixie: grepcidr 2.0-2
- epel 10: grepcidr 2.0-9.el10_0 Β· GPL-2.0-or-later
- epel 9: grepcidr 2.0-1.el9 Β· GPLv2+
- fedora 43: grepcidr 2.0-11.fc43 Β· GPL-2.0-or-later
- fedora 44: grepcidr 2.0-12.fc44 Β· GPL-2.0-or-later
- nixos 26.05: grepcidr 2.0
- nixpkgs unstable: grepcidr 2.0
- opensuse-leap 16.0: grepcidr 2.0-bp160.1.12 Β· GPL-3.0-or-later
- opensuse-leap 16.0: grepcidr 2.0-bp160.1.12 Β· GPL-3.0-or-later
- opensuse-leap 16.0: grepcidr 2.0-bp160.1.12 Β· GPL-3.0-or-later
- opensuse-leap 16.0: grepcidr 2.0-bp160.1.12 Β· GPL-3.0-or-later
- ubuntu jammy: grepcidr 2.0-2
- ubuntu noble: grepcidr 2.0-2
- ubuntu resolute: grepcidr 2.0-2build1
- ubuntu stonking: grepcidr 2.0-2.1 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms