-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
120 lines (99 loc) · 2.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
)
func main() {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
fmt.Printf("\t%s {port (int)}\n", os.Args[0])
fmt.Printf("\tex:\twhoseport 8080\n")
}
flag.Parse()
if len(os.Args) < 2 {
fmt.Printf("error: missing port number\n")
flag.Usage()
os.Exit(1)
}
port, err := strconv.Atoi(flag.Arg(0))
if err != nil {
fmt.Printf("error: port must be an integer\n")
flag.Usage()
os.Exit(1)
}
lsof(port)
}
func lsof(port int) {
c1 := exec.Command("lsof", "-i", fmt.Sprintf(":%d", port))
c2 := exec.Command("grep", "LISTEN")
r, w := io.Pipe()
defer r.Close()
c1.Stdout = w
c2.Stdin = r
var b2 bytes.Buffer
c2.Stdout = &b2
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
d := &data{&b2}
j, err := json.MarshalIndent(d, "", "\t")
if err != nil {
fmt.Fprintf(os.Stderr, "no service found on port %d", port)
}
fmt.Fprintf(os.Stdout, "%s\n", j)
}
type data struct {
values *bytes.Buffer
}
func (i *data) MarshalJSON() ([]byte, error) {
var values []string
spl := bytes.Split(i.values.Bytes(), []byte(" "))
for i, v := range spl {
if len(v) <= 0 {
continue
}
if len(values) == 8 {
values = append(values, strings.TrimSpace(string(bytes.Join(spl[i:], []byte(" ")))))
break
}
values = append(values, strings.TrimSpace(string(v)))
}
if len(values) != 9 {
return nil, fmt.Errorf("incorrect number of values returned")
}
pid, err := strconv.Atoi(values[1])
if err != nil {
return nil, fmt.Errorf("could not convert process id to int: %v", err)
}
p := struct {
Command string `json:"command"`
ID int `json:"id"`
User string `json:"user"`
FD string `json:"fd"`
Type string `json:"type"`
Device string `json:"device"`
SizeOffset string `json:"size_offset"`
Node string `json:"node"`
Name string `json:"name"`
}{
Command: values[0],
ID: pid,
User: values[2],
FD: values[3],
Type: values[4],
Device: values[5],
SizeOffset: values[6],
Node: values[7],
Name: values[8],
}
return json.Marshal(p)
}