Skip to content

Instantly share code, notes, and snippets.

@dlipovetsky
Last active July 9, 2018 06:18
Show Gist options
  • Save dlipovetsky/7f400050cebc51c23c8c4d7ebcfa226a to your computer and use it in GitHub Desktop.
Save dlipovetsky/7f400050cebc51c23c8c4d7ebcfa226a to your computer and use it in GitHub Desktop.
Go - Given an IPv4 CIDR, iterate over all IPs
package main
import (
"log"
"net"
"github.com/apparentlymart/go-cidr/cidr"
)
func main() {
_, ipNet, err := net.ParseCIDR("192.168.0.0/24")
if err != nil {
log.Fatal(err)
}
n := int(cidr.AddressCount(ipNet))
for i := 0; i < n; i++ {
ip, err := cidr.Host(ipNet, i)
if err != nil {
log.Fatal(err)
}
log.Printf("host %d: %v", i, ip)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment