-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (69 loc) · 2.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/brotherpowers/ipsubnet"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("requires at least one subnet argument")
os.Exit(1)
}
total := 0
utotal := 0 //usable
for _, netaddr := range os.Args[1:] {
// testing. I should switch to the net.ParseCIDR parser.
// then use ipnet.Mask.Size() and use the second return, bits. guess i still
// need to convert to a string first...
//
// /32 doesn't display properly, but in ipsubnet lib.
// aip, aipnet, _ := net.ParseCIDR(netaddr)
// fmt.Println(aip, aipnet)
// aones, abits := aipnet.Mask.Size()
// fmt.Println(aones, abits)
// end testing
fields := strings.Split(netaddr, "/")
if len(fields) == 1 {
fields = append(fields, "32")
} else if len(fields) > 2 {
fmt.Println("malformed CIDR address:", netaddr)
continue
}
ip := fields[0]
size, err := strconv.Atoi(fields[1])
if err != nil {
fmt.Printf("invalid subnet size for %s: %s\n", netaddr, err)
continue
}
sub := ipsubnet.SubnetCalculator(ip, size)
if size == 32 {
fmt.Println(netaddr)
fmt.Println("------------------")
fmt.Println("Total IPs: ", 1)
fmt.Println("Usable IPs: ", 1)
fmt.Println("usage IP Range: ", sub.GetIPAddressRange()[0], "-", sub.GetIPAddressRange()[0])
fmt.Println("Network Address: ", sub.GetNetworkPortion())
fmt.Println("Broadcast Address:", sub.GetNetworkPortion())
fmt.Println("Subnet Mask: ", sub.GetSubnetMask())
fmt.Println()
total += 1
utotal += 1
continue
}
fmt.Println(netaddr)
fmt.Println("------------------")
fmt.Println("Total IPs: ", sub.GetNumberIPAddresses())
fmt.Println("Usable IPs: ", sub.GetNumberAddressableHosts())
fmt.Println("usage IP Range: ", sub.GetIPAddressRange()[0], "-", sub.GetIPAddressRange()[1])
fmt.Println("Network Address: ", sub.GetNetworkPortion())
fmt.Println("Broadcast Address:", sub.GetBroadcastAddress())
fmt.Println("Subnet Mask: ", sub.GetSubnetMask())
fmt.Println()
total += sub.GetNumberIPAddresses()
utotal += sub.GetNumberAddressableHosts()
}
fmt.Println("Total: ", total)
fmt.Println("Usable Total: ", utotal)
}