We 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 cases

This 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:

  1. View all non-bogon IPs after scraping a web page with IP content.
  2. Gather all IPs in a web request log to put onto a map to see where our web users come from.
  3. Retrieve only non-bogon IPv6 addresses in a list containing a huge variety of IP addresses and count how many there are.

In other words, you can use grepip to make lists of IP addresses more accessible and flexible.

Filtering IPs

Before diving in, consider that there are at least two ways we know of for filtering IPs:

  1. Use grep with some complex regex.
  2. Use grepip.

Using 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.

Using grepip

grepip  a specialized version of grep specifically for IP filtering. It'll give us a bit more flexibility:

  • No regex needed.
  • Can filter IPv6 addresses simultaneously as well.
  • Can filter out reserved/bogon IP addresses.

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.

Getting started with IPinfo's CLI

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.