Last active
July 9, 2018 06:18
-
-
Save dlipovetsky/7f400050cebc51c23c8c4d7ebcfa226a to your computer and use it in GitHub Desktop.
Go - Given an IPv4 CIDR, iterate over all IPs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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