1GREPCIDR(1) General Commands Manual GREPCIDR(1)
2
3
4
6 grepcidr — Filter IPv4 and IPv6 addresses matching CIDR patterns
7
9 grepcidr [OPTIONS] PATTERN [FILE...]
10
11 grepcidr [OPTIONS] [-e PATTERN | -f PATFILE] [FILE...]
12
14 grepcidr can be used to filter a list of IP addresses against one or
15 more Classless Inter-Domain Routing (CIDR) specifications. As with
16 grep, there are options to invert matching and load patterns from a
17 file. grepcidr is capable of efficiently processing large numbers of
18 IPs and networks.
19
20
22 -V Show software version
23
24 -c Display count of the matching lines, instead of showing the
25 lines
26
27 -i Inverse match, include lines without an IP, implies -v
28
29 -s Enforce strict alignment of CIDR mask; host portion must be
30 all zero
31
32 -v Invert the sense of matching, output lines with IPs that
33 don't match
34
35 -x Strict matching, only look at start of line
36
37 -e Specify individual IP or CIDR pattern(s) on command-line
38
39 -f Load individual IP or CIDR pattern(s) from file
40
42 PATTERN specified on the command line may contain multiple patterns
43 separated by whitespace or commas. For long lists of network patterns,
44 use -f to load a file where each line contains one pattern (can be IPv4
45 or IPv6). Blank lines and comments starting with # are ignored.
46
47
48 Each IPv4 pattern, whether on command line or loaded from a file, may
49 be:
50
51 a.b.c.d/xy (CIDR format)
52 a.b.c.d-e.f.g.h (IP range)
53 a.b.c.d (Single IP)
54
55
56 And similarly for IPv6:
57
58 a:b:c::/xyz (CIDR format)
59 a:b:c:: (Single IP)
60
61
62 Dotted-decimal IPv4 format, or any legal IPv6 format is supported (see:
63 man inet_pton).
64
65
66 IP addresses that appear anywhere on the input line will be compared
67 and matched against the patterns. To be recognized, an IP (field) must
68 end with terminating text. An IPv4 field terminates upon anything
69 other than alphanumeric or dot. An IPv6 field terminates upon anything
70 other than alphanumeric, dot, or colon. This is to prevent accidental
71 matching of ambiguous text such as host names containing reverse DNS.
72
73
74 Use the -x option to do a strict parse without searching the whole
75 line, and grepcidr will only look for the single IP at the start of the
76 line.
77
78
80 grepcidr -f ournetworks blacklist > abuse.log
81
82 Find customers (CIDR ranges in file) that appear in blacklist
83
84 grepcidr 2001:db8::/32 log.1 log.2
85
86 Search for this IPv6 network inside two files
87
88 grepcidr 127.0.0.0/8 iplog
89
90 Searches for any localnet IP addresses inside the iplog file
91
92 grepcidr "192.168.0.1-192.168.10.13" iplog
93
94 Searches for IPs matching indicated range in the iplog file
95
96 script | grepcidr -vf whitelist > blacklist
97
98 Create a blacklist, with whitelisted networks removed (inverse)
99
100 grepcidr -f list1 list2
101
102 Cross-reference two lists, outputs IPs common to both lists
103
105 As with grep: the exit status is 0 if matching IPs are found, and 1 if
106 not found. If an error occurred the exit status is 2.
107
108
110 This software and manual page was written by Jem Berkes <jem@berkes.ca>
111 based on the first man page and DocBook format contributed by Ryan
112 Finnie. Permission is granted to copy, distribute and/or modify this
113 document under the terms of the GNU General Public License, Version 2
114 or any later version published by the Free Software Foundation.
115
116
118 http://www.pc-tools.net/unix/grepcidr/
119
120
122 Sponsored in part by the Spamhaus Project, http://www.spamhaus.org/
123
124
125 Thanks to John Levine <johnl@taugh.com> for sharing his alternative im‐
126 plementation. I used a couple ideas from his code, such as portable
127 128-bit numbers and support for multiple input files. However, John's
128 version is quite different and represents a significant fork in the
129 project.
130
131
132 Thanks to Ryan Finnie <ryan@finnie.org> for his work on the Debian
133 package. I've adopted several of his changes including the Makefile,
134 and the DocBook format which now is the source of the man page. Thanks
135 to Ryan for writing the first manual in DocBook format.
136
137
138 Many thanks to Dick Wesseling <ftu@fi.uu.nl> who suggested an improved
139 data structure format as well as binary search, to improve grepcidr
140 performance.
141
142
143
144
145 GREPCIDR(1)