-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (114 loc) · 3.18 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"log"
"os/exec"
"bytes"
"strconv"
"sort"
"strings"
"runtime"
"github.com/gosuri/uitable"
)
// windows support
// https://commandwindows.com/netstat.htm
// https://stackoverflow.com/questions/18059798/windows-batch-equivalent-of-fuser-k-folder
var version = "HEAD"
func sliceContains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
func trimSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
s = s[:len(s)-len(suffix)]
}
return s
}
func fuserInfo(proto string, port string) []string {
proto = trimSuffix(proto, "6")
cmd := exec.Command("sudo", "fuser", "-a", "-v", fmt.Sprintf("%s/%s", port, proto))
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
lines := strings.Split(out.String(), "\n")
for _, item := range lines {
if strings.HasPrefix(item, port) {
fields := strings.Fields(item)
// fmt.Printf("%+q\n", fields)
return fields
}
}
return []string{}
}
func toInt64(s string) int64 {
x, err := strconv.ParseInt(s, 10, 64)
if err != nil {
fmt.Println(err)
return 0
}
return x
}
func netstat() [][]string {
results := make([][]string, 0)
cmd := exec.Command("sudo", "netstat", "-4", "-6", "--numeric", "--all")
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(stderr.String())
fmt.Println(err)
return results
}
lines := strings.Split(stdout.String(), "\n")
string_results := make([]string, 0)
for _, item := range lines {
if (strings.HasPrefix(item, "tcp") || strings.HasPrefix(item, "udp")) {
fields := strings.Fields(item)
proto := trimSuffix(fields[0], "6")
local_ip_port := fields[3]
addr := strings.Split(local_ip_port, ":")
port := addr[len(addr)-1]
details := fuserInfo(proto, port)
user := details[1]
process := details[4]
// pid := details[2]
id := proto + port + process + user
if !sliceContains(string_results, id) {
results = append(results, []string{proto, port, process, user})
string_results = append(string_results, id)
}
}
}
return results
}
func main() {
if runtime.GOOS != "linux" {
log.Fatal("Only Linux is supported")
}
table := uitable.New()
table.MaxColWidth = 50
table.AddRow("PORT", "PROTO", "PROCESS", "USER")
items := netstat()
// fmt.Println(items)
// return
// sort items by port number ascending
sort.SliceStable(items, func(i, j int) bool {
porti := toInt64(items[i][1])
portj := toInt64(items[j][1])
return porti < portj
})
for _, v := range items {
table.AddRow(v[1], v[0], v[2], v[3])
}
fmt.Println(table)
}