如何找到给定子网的网络地址?

How to find the network address of a given subnet?

我必须在给定的 IP 地址中创建 256 个子网 2001:1001:0ff0::/48,此外我还需要找到第 2、48 和 224 个子网的 IP 地址。

我想知道是否有公式可以做到这一点。

请考虑 IPv6 的文档https://www.rfc-editor.org/rfc/rfc1883. You can find a formula and description on Wikipedia: https://en.wikipedia.org/wiki/IP_address

由于我们在一个关于软件编程的问答论坛上,这里是一个简单程序(bash/zsh shell 脚本)中包含的公式:

for subnet in {0..255}
do
  echo 2001:1001:0ff0:$(printf "%02x00" $subnet)::/$(echo 48 - $(echo 'l(256)/l(2)' | bc -l) / 1 | bc)
done

要获得第 2、48 和 224 个子网,请使用此程序和公式:

for subnet in 1 47 223
do
  echo 2001:1001:0ff0:$(printf "%02x00" $subnet)::/$(echo 48 - $(echo 'l(256)/l(2)' | bc -l) / 1 | bc)
done