/
iproute.go
108 lines (91 loc) · 2.01 KB
/
iproute.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package utils
import (
"bytes"
"github.com/lastbackend/lastbackend/pkg/log"
"os/exec"
"strings"
)
type FDBRule struct {
Mac string
Device string
DST string
Vlan string
Master string
Self bool
Permanent bool
}
func BridgeFDBList() ([]FDBRule, error) {
var rules []FDBRule
fdblcmd := exec.Command("bridge", "fdb")
var stdout, stderr bytes.Buffer
fdblcmd.Stdout = &stdout
fdblcmd.Stderr = &stderr
if err := fdblcmd.Run(); err != nil {
log.Errorf("cmd.Run() failed with %s\n", err.Error())
return rules, err
}
fdbs := strings.Split(string(stdout.Bytes()), "\n")
rules = make([]FDBRule, len(fdbs))
for _, fdb := range fdbs {
rule := BridgeFDBParse(fdb)
if rule.Mac != "" {
rules = append(rules, rule)
}
}
return rules, nil
}
func BridgeFDBParse(r string) FDBRule {
var rule FDBRule
var rl = strings.Fields(r)
if len(rl) == 0 {
return rule
}
if len(strings.Split(rl[0], ":")) != 6 {
return rule
}
rule.Mac = rl[0]
for i := 1; i < len(rl); i++ {
switch rl[i] {
case "dev":
rule.Device = rl[i+1]
i++
break
case "dst":
rule.DST = rl[i+1]
i++
break
case "vlan":
rule.Vlan = rl[i+1]
i++
break
case "master":
rule.Master = rl[i+1]
i++
break
case "self":
rule.Self = true
break
case "permanent":
rule.Permanent = true
break
}
}
return rule
}