Is there any linux command or script that I can invoke to get ip range with subnet, if I pass number of ip's as an argument, like if I say 256 then it should return me 10.0.0.0/24, Lets say I am talking about 10.0.0.0 range as of now.
Asked
Active
Viewed 43 times
-1
-
Stack Overflow is a site for programming and development questions. This question appears to be off-topic because its asking for a software or program recommendation. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Software Recommendations Stack Exchange](http://softwarerecs.stackexchange.com/) would be a better place to ask. – jww Jun 12 '17 at 18:49
1 Answers
0
If you have Perl installed, you can calculate that value with the following script:
perl -le 'print 32-int(log(<>)/log(2));'
This takes input from standard in, so you can pipe your desired number of IPs as follows:
echo 256 | perl -le 'print 32-int(log(<>)/log(2));'
# prints 24 followed by a newline to stdout
If you want it to display an IP before, you can run this Perl program:
echo 256 | perl -le 'print "10.0.0.0/" . (32-int(log(<>)/log(2)));'
# prints 10.0.0.0/24 followed by a newline to stdout
The subnet mask can be calculated by subtracting the log base 2 of the desired number of IPs from 32 (IPv4 netmask length), and that's what the above Perl script does.

Piccolo
- 1,612
- 4
- 22
- 38