-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev.go
More file actions
62 lines (59 loc) · 1.72 KB
/
Copy pathdev.go
File metadata and controls
62 lines (59 loc) · 1.72 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
package main
import (
"encoding/json"
"errors"
"os/exec"
)
type DevInfo struct {
Ifindex int `json:"ifindex"`
Ifname string `json:"ifname"`
Flags []string `json:"flags"`
Mtu int `json:"mtu"`
Qdisc string `json:"qdisc"`
Operstate string `json:"operstate"`
Group string `json:"group"`
Txqlen int `json:"txqlen"`
LinkType string `json:"link_type"`
Address string `json:"address,omitempty"`
Broadcast string `json:"broadcast,omitempty"`
AddrInfo []struct {
Family string `json:"family"`
Local string `json:"local"`
Prefixlen int `json:"prefixlen"`
Scope string `json:"scope"`
Label string `json:"label,omitempty"`
ValidLifeTime int64 `json:"valid_life_time"`
PreferredLifeTime int64 `json:"preferred_life_time"`
} `json:"addr_info"`
Stats64 struct {
Rx struct {
Bytes int `json:"bytes"`
Packets int `json:"packets"`
Errors int `json:"errors"`
Dropped int `json:"dropped"`
OverErrors int `json:"over_errors"`
Multicast int `json:"multicast"`
} `json:"rx"`
Tx struct {
Bytes int `json:"bytes"`
Packets int `json:"packets"`
Errors int `json:"errors"`
Dropped int `json:"dropped"`
CarrierErrors int `json:"carrier_errors"`
Collisions int `json:"collisions"`
} `json:"tx"`
} `json:"stats64"`
}
func GetDevInfo(dev string) (DevInfo, error) {
var devs []DevInfo
cmd := exec.Command("ip", "-s", "-j", "address")
stdout, _ := cmd.StdoutPipe()
cmd.Start()
json.NewDecoder(stdout).Decode(&devs)
for _, d := range devs {
if d.Ifname == dev {
return d, cmd.Wait()
}
}
return DevInfo{}, errors.New("device not found")
}