Divide networks into subnets (by shift or by desired count)
MIT License
Package ipnets
divides net.IPNet
networks into subnets.
Originally forked from mhuisi/ipv4utils, but now repurposed.
go get github.com/dghubble/ipnets
Read GoDoc
Divide a net.IPNet by shifting the subnet bitmask with SubnetShift
.
import (
"net"
"github.com/dghubble/ipnets"
)
...
ip, network, err := net.ParseCIDR("10.2.0.0/16")
subnets, err := ipnets.SubnetShift(network, 1)
fmt.Println(subnets) // [10.2.0.0/17 10.2.128.0/17]
Divide a net.IPNet into a desired number of subnets (or more) with SubnetInto
. If a number is given that is not a power of 2, the returned number of subnets will be the next power of 2.
ip, network, err := net.ParseCIDR("10.2.0.0/16")
subnets, err := ipnets.SubnetInto(network, 4)
fmt.Println(subnets) // [10.2.0.0/18 10.2.64.0/18 10.2.128.0/18 10.2.192.0/18]
Bug fixes are welcome. Please note that we are not accepting feature requests at this time.