Explore our IP Address Database Downloads for instant access to our IP address insights
Learn moreWe recently released the official IPinfo CLI. With features from bulk lookups to summarizing details for up to 1000 IPs, it allows users to query our APIs more quickly. But along with that, our CLI also includes a handy command called grepip
for IP filtering.
grepip
use casesThis tool helps find IPs in arbitrarily large texts and can also help clean up an IP list into usable datasets. Here are several example use cases of grepip
:
In other words, you can use grepip
to make lists of IP addresses more accessible and flexible.
Before diving in, consider that there are at least two ways we know of for filtering IPs:
grep
with some complex regex.grepip
.grep
With grep
, here's a regex that will get the job done most of the time:
$ ip addr | grep -E -o '([0-9]{1,3}\.){3}[0-9]{1,3}'
127.0.0.1
111.111.111.111
192.168.8.101
192.168.8.255
The problem is, this matches with 999.999.999.999
as well, which is not a valid IPv4 address.
Here's another one that's going to exclude strings whose numbers are out-of-range:
$ ip addr | grep -E -o '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
127.0.0.1
111.111.111.111
192.168.8.101
192.168.8.255
You can alias this to make it easier to access:
$ alias grepipv4="grep -E -o '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'"
$ ip addr | grepipv4
127.0.0.1
111.111.111.111
192.168.8.101
192.168.8.255
The problem is, if you want to change the options, you'd have to type it manually anyway or create a bash function instead of an alias.
grepip
grepip
a specialized version of grep
specifically for IP filtering. It'll give us a bit more flexibility:
If you don't have it, first install it:
# using homebrew?
$ brew install grepip
# using debian/ubuntu?
$ curl -Ls https://github.com/ipinfo/cli/releases/download/grepip-1.1.0/deb.sh | sh
# using go get?
$ go get github.com/ipinfo/cli/grepip
# something else? see https://github.com/ipinfo/cli#installation
Let's see how to do the same IPv4 filtering as above with grepip
$ ip addr | grepip -o -4 127.0.0.1 111.111.111.111 192.168.8.101 192.168.8.255
If we wanted to only know about non-reserved IPs, just use the -x
or --exclude-reserved
flag:
$ ip addr | grepip -o -4 127.0.0.1 111.111.111.111 192.168.8.101 192.168.8.255
If you remove the -4
flag which forces only IPv4 filtering, you can get IPv6 matches as well.
Type grepip --help
for more details.
All CLI binaries (e.g. ipinfo, grepip) are available for download from multiple mechanisms. See https://github.com/ipinfo/cli#installation for full details.
IPinfo is a comprehensive IP address data and API provider with flexible pricing plans to meet your business needs. We handle billions of API requests per month, serving data like IP geolocation, VPN detection, ASN, and more. Sign up for a free account or contact our team to learn more.