Comments (2)
The following code does what you are describing:
package main
import (
"fmt"
"github.com/seancfoley/ipaddress-go/ipaddr"
)
func main() {
listSubnetsExclude("2001:db8::/32", 64, []string{"2001:db8:0:4::/64", "2001:db8:0:2::/64"})
}
func listSubnetsExclude(original string, newPrefix int, excludeAddrs []string) {
subnet := ipaddr.NewIPAddressString(original).GetAddress()
newSubnets := []*ipaddr.IPAddress{subnet.SetPrefixLen(newPrefix)}
for _, addr := range excludeAddrs {
var result []*ipaddr.IPAddress
removeAddr := ipaddr.NewIPAddressString(addr).GetAddress()
for _, s := range newSubnets {
result = append(result, s.Subtract(removeAddr)...)
}
newSubnets = result
}
fmt.Println(newSubnets)
//iterateSubnets(newSubnets) // see below
}
Output:
[2001:db8:1-ffff:*::/64 2001:db8:0:0-1::/64 2001:db8:0:3::/64 2001:db8:0:5-ffff::/64]
You could iterate on those subnets as in the listSubnets
function above, but that's a very large number of subnets, about 2 to the power of 32, more than a billion. So you would not want to iterate through the whole thing.
func iterateSubnets(newSubnets []*ipaddr.IPAddress) {
i := 0
top:
for _, s := range newSubnets {
iterator := s.PrefixIterator()
for iterator.HasNext() {
net := iterator.Next()
fmt.Print(net, " ")
i++
if i > 10 {
break top
}
}
}
fmt.Println("...")
}
Output:
2001:db8:1::/64 2001:db8:1:1::/64 2001:db8:1:2::/64 2001:db8:1:3::/64 2001:db8:1:4::/64 2001:db8:1:5::/64 2001:db8:1:6::/64 2001:db8:1:7::/64 2001:db8:1:8::/64 2001:db8:1:9::/64 2001:db8:1:a::/64 ...
On a more general level, you might wish to look at a couple examples of subnetting:
https://github.com/seancfoley/ipaddress-go/wiki/Code-Examples-3:-Subnetting-and-Other-Subnet-Operations#variable-length-subnetting and https://github.com/seancfoley/ipaddress-go/wiki/Code-Examples-3:-Subnetting-and-Other-Subnet-Operations#automatic-subnetting
from ipaddress-go.
Thank you
from ipaddress-go.
Related Issues (9)
- How to allocate next available free subnet HOT 16
- How to find remaining IP space in a CIDR HOT 2
- How to Convert CIDR to IP Range ? HOT 1
- ipaddr v1.5.2 bintree error HOT 2
- staticcheck problem HOT 1
- "main redeclared in this block" HOT 2
- internal compile rerror HOT 3
- Trie is no longer DeepEqual after v1.5.5 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ipaddress-go.