-
Notifications
You must be signed in to change notification settings - Fork 0
/
arp.go
57 lines (46 loc) · 1.26 KB
/
arp.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
package juniper
import (
"encoding/xml"
"github.com/Juniper/go-netconf/netconf"
"github.com/g-portal/switchmgr-go/pkg/models"
"strings"
)
func ParseArpTable(data []byte) ([]models.ArpEntry, error) {
var table junosArpTable
if err := xml.Unmarshal(data, &table); err != nil {
return nil, err
}
entries := make(map[string][]models.MacAddress)
for _, entry := range table.Entries {
entryName := strings.TrimSuffix(strings.TrimSpace(entry.Interfaces[0].Interface), ".0")
if strings.HasPrefix(entryName, "esi.") {
continue
}
if len(entry.Interfaces) == 0 || !isValidInterface(entryName) {
continue
}
if _, ok := entries[entryName]; !ok {
entries[entryName] = make([]models.MacAddress, 0)
}
mac := entry.MacAddress
if !mac.Valid() {
continue
}
entries[entryName] = append(entries[entryName], mac)
}
result := make([]models.ArpEntry, 0)
for port, macs := range entries {
result = append(result, models.ArpEntry{
SwitchPort: port,
MacAddresses: macs,
})
}
return result, nil
}
func (j *Juniper) ListArpTable() ([]models.ArpEntry, error) {
reply, err := j.session.Exec(netconf.RawMethod("<get-ethernet-switching-table-information/>"))
if err != nil {
return nil, err
}
return ParseArpTable([]byte(reply.RawReply))
}