I'm trying to create script that I can input a set of prefixes, which will then list all IP addresses within the prefixes (including network/host/broadcast).
An example would be:
./convert-prefix-to-IPs.sh 192.168.0.0/23 203.20.0.0/16
192.168.0.0
192.168.0.1
...
192.168.0.255
192.168.1.0
..
192.168.1.255
203.20.0.0
..
203.20.255.255
There are some python/perl scripts which can do this, but I'm hoping to have a simple bash script, as it may be used on systems without perl/python (yes.. i know.. )
This short script will print all the IP addresses in a CIDR range in a few lines of Bash. (I named it
prips
after the Ubuntu command of the same name. Obviously, if that command is available, use that.)Note that I assume the RedHat Linux (Erik Troan, Preston Brown) version of
ipcalc
, not the Krischan Jodies version that is installed on some platforms (e.g. Mac OS X).Examples:
Calculates correct number of addresses in a /23 networks:
Inspecting a few of those addresses using cut:
And maybe too slow but also correctly generates the 16 million addresses in a /8 network:
Wanted to comment on an answer above but don't have the rep yet.
Using the top solution with NMAP I added this to my
.bashrc
Now I can use this with just
expand-ip targs
.I too was looking for this solution and found that @scherand script worked great. I also have added to this script to give you more option. Help File below.
THIS SCRIPT WILL EXPAND A CIDR ADDRESS.
SYNOPSIS
DESCRIPTION
EXAMPLES
I recently wrote a function to generate all IP addresses from a given network address. The function takes the network address as argument and accepts CIDR and subnet masks. The script then stores all IPs in the array variable $ips.
Code
Example
Here is what I use to generate all the IP addresses in a given CIDR block
Just that simple
The above command outputs this