forked from weaveworks/procspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netstat.go
83 lines (65 loc) · 1.5 KB
/
netstat.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
package procspy
// netstat reading.
import (
"net"
"strconv"
"strings"
)
// parseDarwinNetstat parses netstat output. (Linux has ip:port, darwin
// ip.port. The 'Proto' column value also differs.)
func parseDarwinNetstat(out string) []Connection {
//
// Active Internet connections
// Proto Recv-Q Send-Q Local Address Foreign Address (state)
// tcp4 0 0 10.0.1.6.58287 1.2.3.4.443 ESTABLISHED
//
res := []Connection{}
for i, line := range strings.Split(out, "\n") {
if i == 0 || i == 1 {
// Skip header
continue
}
// Fields are:
fields := strings.Fields(line)
if len(fields) != 6 {
continue
}
if fields[5] != "ESTABLISHED" {
continue
}
t := Connection{
Transport: "tcp",
}
// Format is <ip>.<port>
locals := strings.Split(fields[3], ".")
if len(locals) < 2 {
continue
}
var (
localAddress = strings.Join(locals[:len(locals)-1], ".")
localPort = locals[len(locals)-1]
)
t.LocalAddress = net.ParseIP(localAddress)
p, err := strconv.Atoi(localPort)
if err != nil {
return nil
}
t.LocalPort = uint16(p)
remotes := strings.Split(fields[4], ".")
if len(remotes) < 2 {
continue
}
var (
remoteAddress = strings.Join(remotes[:len(remotes)-1], ".")
remotePort = remotes[len(remotes)-1]
)
t.RemoteAddress = net.ParseIP(remoteAddress)
p, err = strconv.Atoi(remotePort)
if err != nil {
return nil
}
t.RemotePort = uint16(p)
res = append(res, t)
}
return res
}