-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (41 loc) · 936 Bytes
/
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
package main
import (
"flag"
"fmt"
"github.com/raspi/ip-range-to-CIDR/lib"
"net"
"os"
)
var VERSION = `0.0.0`
var BUILD = `dev`
var BUILDDATE = `0000-00-00T00:00:00+00:00`
const AUTHOR = `Pekka Järvinen`
func main() {
flag.Usage = func() {
fmt.Printf(`IP range to CIDR v%v build %v`+"\n", VERSION, BUILD)
fmt.Printf(`(c) %v 2019-`+"\n", AUTHOR)
fmt.Println(`Usage:`)
fmt.Printf(` %v <start IP> <end IP>`+"\n", os.Args[0])
fmt.Println()
fmt.Println(`Example:`)
fmt.Printf(` %v 192.168.0.0 192.168.0.255`+"\n", os.Args[0])
fmt.Printf(` Output: 192.168.0.0/24` + "\n")
}
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
if flag.NArg() != 2 {
flag.Usage()
os.Exit(1)
}
start := net.ParseIP(flag.Arg(0))
end := net.ParseIP(flag.Arg(1))
ip, mask, err := lib.ToCidr(start, end)
if err != nil {
fmt.Printf(`error: %v`, err)
os.Exit(1)
}
fmt.Printf(`%s/%d`, ip, mask)
}