-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCIDRxform.go
More file actions
80 lines (69 loc) · 1.63 KB
/
CIDRxform.go
File metadata and controls
80 lines (69 loc) · 1.63 KB
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
79
80
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"strings"
)
func CIDRtoSubNet(cidr string) (string, error) {
// Parse the CIDR notation
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return "", err
}
// Convert the subnet mask to dotted decimal format
subnetMask := net.IP(ipnet.Mask).String()
// Combine IP address and subnet mask
return fmt.Sprintf("%s|%s", ip.String(), subnetMask), nil
}
func main() {
// Example CIDR notation
//cidr := "192.168.1.0/27"
//make sure filename is given on command line
if len(os.Args) < 2 {
log.Fatalf("Usage: %s <filename>", os.Args[0])
}
// Get the filename from the cmd line
filename := os.Args[1]
// Open the file
file, err := os.Open(filename)
if err != nil {
log.Fatalf("Failed to open file: %s", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
//slice to hold each line
var lines []string
// var FULL_
//Read each line
N := 0
for scanner.Scan() {
//fget a line from the file
lines = append(lines, scanner.Text())
// break the line into parts
lineparts := strings.Split(lines[N], "|")
if len(lineparts) != 3 {
fmt.Println("Error: the line does not contain 3 parts")
return
}
VLAN := lineparts[0]
CIDR := lineparts[1]
DESCR := lineparts[2]
//fmt.Println(VLAN, ",", CIDR, ",", DESCR)
// Convert to subnet mask notation
subnet, err := CIDRtoSubNet(CIDR)
if err != nil {
fmt.Println("Error:", err)
return
}
//combine all the fields
fmt.Print(VLAN, "|", subnet, "|", DESCR, "\n")
N++
}
// Check for errors reading the line
if err := scanner.Err(); err != nil {
log.Fatalf("error reading from file: %s", err)
}
}