From d5bd1b8a365ce59e86d4695ed7e0de84d641126c Mon Sep 17 00:00:00 2001 From: ben-auo Date: Tue, 1 Oct 2024 21:33:42 -0700 Subject: [PATCH 1/5] fixes #5 --- cidr2ip.go | 43 ++++++++++--------------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/cidr2ip.go b/cidr2ip.go index c5fe7e8..163a880 100644 --- a/cidr2ip.go +++ b/cidr2ip.go @@ -8,6 +8,8 @@ import ( "net" "os" "regexp" + + "github.com/adedayo/cidr" ) const ( @@ -59,49 +61,24 @@ func main() { cidrs = args } - for _, cidr := range cidrs { - displayIPs(cidr) + for _, cs := range cidrs { + displayIPs(cs) } } else { // no piped input, no file provide and no args, display usage flag.Usage() } } -func isIPAddr(cidr string) bool { - match, _ := regexp.MatchString(IPRegex, cidr) +func isIPAddr(cs string) bool { + match, _ := regexp.MatchString(IPRegex, cs) return match } -func displayIPs(cidr string) { - var ips []string - - // if a IP address, display the IP address and return - if isIPAddr(cidr) { - fmt.Println(cidr) - return - } - - ipAddr, ipNet, err := net.ParseCIDR(cidr) - if err != nil { - log.Print(err) - return - } +func displayIPs(cs string) { + ips := cidr.Expand(cs) - for ip := ipAddr.Mask(ipNet.Mask); ipNet.Contains(ip); increment(ip) { - ips = append(ips, ip.String()) - } - - // CIDR too small eg. /31 - if len(ips) <= 2 { - return - } - - if *printRangesPtrPtr == true { - fmt.Printf("%s-%s\n", ips[1], ips[len(ips)-2]) - } else { - for _, ip := range ips[1 : len(ips)-1] { - fmt.Println(ip) - } + for _, ip := range ips { + println(ip) } } From faa847ec9d6f031514df6aa1e02f1d5be53a344f Mon Sep 17 00:00:00 2001 From: ben-auo Date: Tue, 1 Oct 2024 22:07:02 -0700 Subject: [PATCH 2/5] cleanup --- cidr2ip.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/cidr2ip.go b/cidr2ip.go index 163a880..765206a 100644 --- a/cidr2ip.go +++ b/cidr2ip.go @@ -82,17 +82,6 @@ func displayIPs(cs string) { } } -// The next IP address of a given ip address -// https://stackoverflow.com/a/33925954 -func increment(ip net.IP) { - for i := len(ip) - 1; i >= 0; i-- { - ip[i]++ - if ip[i] != 0 { - break - } - } -} - func usage() { fmt.Fprintf(os.Stderr, "CIDR to IPs version %s\n", Version) fmt.Fprintf(os.Stderr, "Usage: $ cidr2ip [-r] [-f ] \n") From ec43d0dbb8ca3cc40c52997fff46aba3959457d1 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 1 Oct 2024 22:31:42 -0700 Subject: [PATCH 3/5] cleanup --- Makefile | 1 + cidr2ip.go | 1 - go.mod | 5 +++++ go.sum | 3 +++ 4 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/Makefile b/Makefile index 815c8ac..0fca875 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,5 @@ all: GOOS=windows GOARCH=amd64 go build -o binaries/cidr2ip-win64.exe cidr2ip.go GOOS=linux GOARCH=386 go build -o binaries/cidr2ip-linux32 cidr2ip.go GOOS=linux GOARCH=amd64 go build -o binaries/cidr2ip-linux64 cidr2ip.go + GOOS=linux GOARCH=arm64 go build -o binaries/cidr2ip-linuxarm64 cidr2ip.go GOOS=darwin GOARCH=amd64 go build -o binaries/cidr2ip-osx64 cidr2ip.go diff --git a/cidr2ip.go b/cidr2ip.go index 765206a..7a821d4 100644 --- a/cidr2ip.go +++ b/cidr2ip.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "log" - "net" "os" "regexp" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5932089 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module cidr2ip + +go 1.23.2 + +require github.com/adedayo/cidr v0.1.5 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b1bdcc4 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +github.com/adedayo/cidr v0.1.5 h1:O6N8M2CPOT7LAy2upOHnQ4YIKHD+VXAQc8/OalCsmnU= +github.com/adedayo/cidr v0.1.5/go.mod h1:By6g82fmUcv8/Z/6JcDs1D4wO4gc/Ookb842bVCV6Io= +github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= From 06f647b82a77dfc8ffbdf0f017d77cb9a6ba6dd7 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 2 Oct 2024 20:47:36 -0700 Subject: [PATCH 4/5] fixes #6 --- cidr2ip.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/cidr2ip.go b/cidr2ip.go index 7a821d4..baec8c7 100644 --- a/cidr2ip.go +++ b/cidr2ip.go @@ -12,15 +12,18 @@ import ( ) const ( - Version = "2.0.0" + Version = "3.0.0" IPRegex = `\b(?:\d{1,3}\.){3}\d{1,3}\b$` ) var ( - cidrFilePtr = flag.String("f", "", - "[Optional] Name of file with CIDR blocks") + cidrFilePtr = flag.String("i", "", + "[Optional] Name of input file with CIDR blocks") + outputFilePtr = flag.String("o", "", + "[Optional] Name of output file for storing result") printRangesPtrPtr = flag.Bool("r", false, "[Optional] Print IP ranges instead of all IPs") + outputWriterPtr *bufio.Writer ) func main() { @@ -33,6 +36,16 @@ func main() { } args := os.Args[1:] + if *outputFilePtr != "" { + file, err := os.Create(*outputFilePtr) + if err != nil { + log.Fatal(err) + } else { + defer file.Close() + outputWriterPtr = bufio.NewWriter(file) + } + } + if *cidrFilePtr != "" { file, err := os.Open(*cidrFilePtr) if err != nil { @@ -66,6 +79,10 @@ func main() { } else { // no piped input, no file provide and no args, display usage flag.Usage() } + + if outputWriterPtr != nil { + outputWriterPtr.Flush() + } } func isIPAddr(cs string) bool { @@ -77,7 +94,11 @@ func displayIPs(cs string) { ips := cidr.Expand(cs) for _, ip := range ips { - println(ip) + if *outputFilePtr != "" { + outputWriterPtr.WriteString(ip + "\n") + } else { + println(ip) + } } } From af4da679a23f77b6f4db773729fbf46daf06b8d1 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 2 Oct 2024 22:09:15 -0700 Subject: [PATCH 5/5] removed range --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++--------- cidr2ip.go | 24 +++++++++--------------- 2 files changed, 55 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index b8b5553..8d15f71 100644 --- a/README.md +++ b/README.md @@ -7,32 +7,69 @@ This program converts IPv4 CIDR blocks into their constituent IP addresses. 1. Commnd line arguments ``` code@express:~$ cidr2ip 10.0.0.0/30 192.68.0.0/30 +10.0.0.0 10.0.0.1 10.0.0.2 +10.0.0.3 +192.68.0.0 192.68.0.1 192.68.0.2 -``` - -The `-r` flag outputs IP ranges seperated by hyphen. - -``` -code@express:~$ cidr2ip -r 10.0.0.0/30 192.68.0.0/30 -10.0.0.1-10.0.0.2 -192.68.0.1-192.68.0.2 +192.68.0.3 ``` 2. Piped input ``` code@express:~$ cat cidrs.txt | cidr2ip +127.0.0.1 +192.168.0.100 192.168.0.101 192.168.0.102 +192.168.0.103 +10.0.0.0 +10.0.0.1 +10.0.0.2 +10.0.0.3 +10.0.0.4 +10.0.0.5 +10.0.0.6 +10.0.0.7 ``` 3. File input ``` -code@express:~$ cidr2ip -f cidrs.txt +code@express:~$ cidr2ip -i cidrs.txt +127.0.0.1 +192.168.0.100 192.168.0.101 192.168.0.102 +192.168.0.103 +10.0.0.0 +10.0.0.1 +10.0.0.2 +10.0.0.3 +10.0.0.4 +10.0.0.5 +10.0.0.6 +10.0.0.7 +``` + +4. File output +``` +code@express:~$ cidr2ip -i cidrs.txt -o results.txt +code@express:~$ cat results.txt +127.0.0.1 +192.168.0.100 +192.168.0.101 +192.168.0.102 +192.168.0.103 +10.0.0.0 +10.0.0.1 +10.0.0.2 +10.0.0.3 +10.0.0.4 +10.0.0.5 +10.0.0.6 +10.0.0.7 ``` ### Install diff --git a/cidr2ip.go b/cidr2ip.go index baec8c7..07eb8c2 100644 --- a/cidr2ip.go +++ b/cidr2ip.go @@ -21,8 +21,6 @@ var ( "[Optional] Name of input file with CIDR blocks") outputFilePtr = flag.String("o", "", "[Optional] Name of output file for storing result") - printRangesPtrPtr = flag.Bool("r", false, - "[Optional] Print IP ranges instead of all IPs") outputWriterPtr *bufio.Writer ) @@ -37,12 +35,12 @@ func main() { args := os.Args[1:] if *outputFilePtr != "" { - file, err := os.Create(*outputFilePtr) + f, err := os.Create(*outputFilePtr) if err != nil { log.Fatal(err) } else { - defer file.Close() - outputWriterPtr = bufio.NewWriter(file) + defer f.Close() + outputWriterPtr = bufio.NewWriter(f) } } @@ -67,11 +65,7 @@ func main() { } } else if len(args) > 0 { // look for CIDRs on cmd line var cidrs []string - if *printRangesPtrPtr == true { - cidrs = args[1:] - } else { - cidrs = args - } + cidrs = args for _, cs := range cidrs { displayIPs(cs) @@ -104,11 +98,11 @@ func displayIPs(cs string) { func usage() { fmt.Fprintf(os.Stderr, "CIDR to IPs version %s\n", Version) - fmt.Fprintf(os.Stderr, "Usage: $ cidr2ip [-r] [-f ] \n") - fmt.Fprintf(os.Stderr, "Example: $ cidr2ip -f cidrs.txt\n") - fmt.Fprintf(os.Stderr, " $ cidr2ip 10.0.0.0/24\n") - fmt.Fprintf(os.Stderr, " $ cidr2ip -r 10.0.0.0/24\n") - fmt.Fprintf(os.Stderr, " $ cidr2ip -r -f cidrs.txt\n") + fmt.Fprintf(os.Stderr, "Usage: $ cidr2ip [-i ] [-o ] [] \n") + fmt.Fprintf(os.Stderr, "Example: $ cidr2ip 10.0.0.0/27\n") + fmt.Fprintf(os.Stderr, " $ cidr2ip -i cidrs.txt\n") + fmt.Fprintf(os.Stderr, " $ cidr2ip -o results.txt 10.0.0.0/27\n") + fmt.Fprintf(os.Stderr, " $ cidr2ip -i cidrs.txt -o results.txt\n") fmt.Fprintf(os.Stderr, " $ cat cidrs.txt | cidr2ip \n") fmt.Fprintf(os.Stderr, "--------------------------\nFlags:\n") flag.PrintDefaults()