This is a small collection of tools and examples for working with IP addresses and ranges in Bash. This post is not meant to be a complete reference, rather a practical how-to guide.

The basic IP regex looks something like this:

This will match an IP address or something that looks a lot like it. If you want only valid IP addresses, the regex becomes a bit longer:

You can find more examples here. A very useful utility is cidr2regex – a Python script that will convert CIDR notation to a Python-compatible regex. It will not be the prettiest or most efficient regex, but it will get the job done right. Here’s an example:

Another helpful utility is grepcidr. To install from source:

Example:

The ipcalc is a simple but helpful utility for, say, configuring NICs. Here’s a quick example:

A similar but better tool is sipcalc (yum install sipcalc):

Here are a few examples of generating various IP ranges (useful for firewall configurations, blacklists, etc).

Using seq:

Using bash brace expansion:

 

Leave A Reply

Please enter your comment!
Please enter your name here