Is there any linux command/program that can tell me subnet range if i provide the number of ip's?

时间:2017-06-12 16:45:38

标签: linux ip subnet

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.

1 个答案:

答案 0 :(得分:0)

如果安装了Perl,则可以使用以下脚本计算该值:

perl -le 'print 32-int(log(<>)/log(2));'

这取自标准输入,因此您可以按如下方式管道所需数量的IP:

echo 256 | perl -le 'print 32-int(log(<>)/log(2));'
# prints 24 followed by a newline to stdout

如果您希望它之前显示IP,您可以运行此Perl程序:

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

可以通过从32(IPv4网络掩码长度)中减去所需数量的IP的日志基数2来计算子网掩码,这就是上述Perl脚本所做的事情。