如果我提供 ip 的数量,是否有 linux command/program 可以告诉我子网范围?

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

有没有我可以调用的任何 linux 命令或脚本来获取子网的 ip 范围,如果我将 ip 的数量作为参数传递,比如我说 256 那么它应该 return我是 10.0.0.0/24,假设我现在谈论的是 10.0.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 脚本所做的。