Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-auo authored Oct 2, 2024
1 parent d2c7db8 commit d5bd1b8
Showing 1 changed file with 10 additions and 33 deletions.
43 changes: 10 additions & 33 deletions cidr2ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net"
"os"
"regexp"

"github.com/adedayo/cidr"
)

const (
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit d5bd1b8

Please sign in to comment.