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.. )
nmap
is useful, but an overkill.You can use
prips
instead. Saves you the hassle of grepping out the extra output fromnmap
and usingawk
.Calling
prips 192.168.0.0/23
will print what you need.I use the following to skip the network address and broadcast:
prips "$subnet" | sed -e '1d; $d'
Prips also has other useful options, e.g. being able to sample every n-th IP.
It's available via
apt
,brew
,rpm
and astar.gz
.I think this little script I hacked together does the trick. If not, it's definitely a starting point! Good luck.
Simplicity works best
I have extended @rberg script a little.
-f
to skip the check)/24
Maybe this is of use for someone.
You can use this script
(you need to have "bc" installed on your system):
This script should do. It's (almost) pure Bash. The
seq
part can be replaced if a completely pure bash is required.Since Bash apparently uses signed two-complement 4-byte integers, the script is limited to /8 mask maximum. I found ranges larger than /16 impractical anyway so this doesn't bother me at all. If someone knows a simple way to overcome this, please share :)
Usage:
Tested with Bash version 4.4.23. YMMV.