Created
October 21, 2019 15:52
-
-
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
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 ( | |
"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