Skip to content

Instantly share code, notes, and snippets.

@lc
Created October 21, 2019 15:52
Show Gist options
  • Save lc/aa3aa6a981a50adde41188988dc4e450 to your computer and use it in GitHub Desktop.
Save lc/aa3aa6a981a50adde41188988dc4e450 to your computer and use it in GitHub Desktop.
get all ip's from a netblock / cidr – edited from https://gist.github.com/kotakanbe/d3059af990252ba89a82
package main
import (
"fmt"
"log"
"net"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s cidr\n", os.Args[0])
os.Exit(1)
}
ip, ipnet, err := net.ParseCIDR(os.Args[1])
if err != nil {
log.Fatal(err)
}
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
fmt.Println(ip)
}
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment