|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net" |
| 9 | + "os" |
| 10 | + "regexp" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + Version = "1.0.0" |
| 15 | + IPRegex = `\b(?:\d{1,3}\.){3}\d{1,3}\b$` |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + cidrFilePtr = flag.String("f", "", |
| 20 | + "[Optional] Name of file with CIDR blocks") |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + flag.Usage = usage |
| 25 | + flag.Parse() |
| 26 | + |
| 27 | + info, err := os.Stdin.Stat() |
| 28 | + if err != nil { |
| 29 | + log.Fatal(err) |
| 30 | + } |
| 31 | + args := os.Args[1:] |
| 32 | + |
| 33 | + if *cidrFilePtr != "" { |
| 34 | + file, err := os.Open(*cidrFilePtr) |
| 35 | + if err != nil { |
| 36 | + log.Fatal(err) |
| 37 | + } |
| 38 | + defer file.Close() |
| 39 | + |
| 40 | + scanner := bufio.NewScanner(file) |
| 41 | + for scanner.Scan() { |
| 42 | + displayIPs(scanner.Text()) |
| 43 | + if err := scanner.Err(); err != nil { |
| 44 | + log.Fatal(err) |
| 45 | + } |
| 46 | + } |
| 47 | + } else if info.Mode()&os.ModeNamedPipe != 0 { // data is piped in |
| 48 | + scanner := bufio.NewScanner(os.Stdin) |
| 49 | + for scanner.Scan() { |
| 50 | + displayIPs(scanner.Text()) |
| 51 | + } |
| 52 | + } else if len(args) > 0 { // look for CIDRs on cmd line |
| 53 | + for _, ip := range args { |
| 54 | + displayIPs(ip) |
| 55 | + } |
| 56 | + } else { // no piped input, no file provide and no args, display usage |
| 57 | + flag.Usage() |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func isIPAddr(cidr string) bool { |
| 62 | + match, _ := regexp.MatchString(IPRegex, cidr) |
| 63 | + return match |
| 64 | +} |
| 65 | + |
| 66 | +func displayIPs(cidr string) { |
| 67 | + var ips []string |
| 68 | + |
| 69 | + // if a IP address, display the IP address and return |
| 70 | + if isIPAddr(cidr) { |
| 71 | + fmt.Println(cidr) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + ipAddr, ipNet, err := net.ParseCIDR(cidr) |
| 76 | + if err != nil { |
| 77 | + log.Print(err) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + for ip := ipAddr.Mask(ipNet.Mask); ipNet.Contains(ip); increment(ip) { |
| 82 | + ips = append(ips, ip.String()) |
| 83 | + } |
| 84 | + |
| 85 | + // CIDR too small eg. /31 |
| 86 | + if len(ips) <= 2 { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + for _, ip := range ips[1 : len(ips)-1] { |
| 91 | + fmt.Println(ip) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// The next IP address of a given ip address |
| 96 | +// https://stackoverflow.com/a/33925954 |
| 97 | +func increment(ip net.IP) { |
| 98 | + for i := len(ip) - 1; i >= 0; i-- { |
| 99 | + ip[i]++ |
| 100 | + if ip[i] != 0 { |
| 101 | + break |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func usage() { |
| 107 | + fmt.Fprintf(os.Stderr, "CIDR to IPs version %s\n", Version) |
| 108 | + fmt.Fprintf(os.Stderr, "Usage: $ cidr2ip [-f <filename>] <list of cidrs> \n") |
| 109 | + fmt.Fprintf(os.Stderr, "Example: $ cidr2ip -f cidrs.txt\n") |
| 110 | + fmt.Fprintf(os.Stderr, " $ cidr2ip 10.0.0.0/24\n") |
| 111 | + fmt.Fprintf(os.Stderr, " $ cat cidrs.txt | cidr2ip \n") |
| 112 | + fmt.Fprintf(os.Stderr, "--------------------------\nFlags:\n") |
| 113 | + flag.PrintDefaults() |
| 114 | +} |
0 commit comments