-
Notifications
You must be signed in to change notification settings - Fork 153
/
00_kill_process.go
237 lines (207 loc) · 5.92 KB
/
00_kill_process.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
func main() {
var (
w = os.Stdout
sudo = true
// socket = "tcp"
socket = "tcp6"
program = ""
// program = "bin/etcd"
// port = ""
port = ":8080"
)
ps, err := NetStat(w, sudo, socket, program, port)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
Kill(w, ps...)
/*
[NetStat] socket: 'tcp6' / program: '00_hello_world' / host: '::' / port: ':8080' / pid: '9768'
[Kill] syscall.Kill -> socket: 'tcp6' / program: '00_hello_world' / host: '::' / port: ':8080' / pid: '9768'
[Kill] Done!
*/
}
// Process describes OS processes.
type Process struct {
Socket string
Program string
Host string
Port string
PID int
}
func (p Process) String() string {
return fmt.Sprintf("socket: '%s' / program: '%s' / host: '%s' / port: '%s' / pid: '%d'",
p.Socket,
p.Program,
p.Host,
p.Port,
p.PID,
)
}
// Kill kills all processes in arguments.
func Kill(w io.Writer, ps ...Process) {
defer func() {
if err := recover(); err != nil {
fmt.Fprintln(w, "[Kill - panic]", err)
}
}()
for _, v := range ps {
fmt.Fprintf(w, "[Kill] syscall.Kill -> %s\n", v)
if err := syscall.Kill(v.PID, syscall.SIGINT); err != nil {
fmt.Fprintln(w, "[Kill - error]", err)
}
}
fmt.Fprintln(w, "[Kill] Done!")
}
/*
NetStat parses the output of netstat command in linux.
Pass '' or '*' to match all. For example, call Kill("tcp", "bin/etcd", "*")
to kill all processes that are running "bin/etcd":
netstat -tlpn
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:2379 0.0.0.0:* LISTEN 21524/bin/etcd
tcp 0 0 127.0.0.1:22379 0.0.0.0:* LISTEN 21526/bin/etcd
tcp 0 0 127.0.0.1:22380 0.0.0.0:* LISTEN 21526/bin/etcd
tcp 0 0 127.0.0.1:32379 0.0.0.0:* LISTEN 21528/bin/etcd
tcp 0 0 127.0.0.1:12379 0.0.0.0:* LISTEN 21529/bin/etcd
tcp 0 0 127.0.0.1:32380 0.0.0.0:* LISTEN 21528/bin/etcd
tcp 0 0 127.0.0.1:12380 0.0.0.0:* LISTEN 21529/bin/etcd
tcp 0 0 127.0.0.1:53697 0.0.0.0:* LISTEN 2608/python2
tcp6 0 0 :::8555 :::* LISTEN 21516/goreman
Otherwise, you would have to run something like the following:
sudo kill $(sudo netstat -tlpn | perl -ne 'my @a = split /[ \/]+/; print "$a[6]\n" if m/:12379/gio');
sudo kill $(sudo netstat -tlpn | perl -ne 'my @a = split /[ \/]+/; print "$a[6]\n" if m/:22379/gio');
sudo kill $(sudo netstat -tlpn | perl -ne 'my @a = split /[ \/]+/; print "$a[6]\n" if m/:32379/gio');
sudo kill $(sudo netstat -tlpn | perl -ne 'my @a = split /[ \/]+/; print "$a[6]\n" if m/:2379/gio');
sudo kill $(sudo netstat -tlpn | perl -ne 'my @a = split /[ \/]+/; print "$a[6]\n" if m/:8080/gio');
*/
func NetStat(w io.Writer, sudo bool, socket, program, port string) ([]Process, error) {
socket = strings.TrimSpace(socket)
program = strings.TrimSpace(program)
if program == "" {
program = "*"
}
port = strings.TrimSpace(port)
if port == "" {
port = "*"
}
if port != "*" {
if !strings.HasPrefix(port, ":") {
port = ":" + port
}
}
if program == "*" && port == "*" {
fmt.Fprintln(w, "[NetStat - warning] grepping all programs.")
}
var flag string
flagFormat := "-%slpn"
switch socket {
case "tcp":
flag = fmt.Sprintf(flagFormat, "t")
case "tcp6":
flag = fmt.Sprintf(flagFormat, "t")
case "tcp*": // match all tcp connections
flag = fmt.Sprintf(flagFormat, "t")
case "udp":
flag = fmt.Sprintf(flagFormat, "u")
default:
return nil, fmt.Errorf("socket '%s' is unknown", socket)
}
cmd := exec.Command("netstat", flag)
if sudo {
cmd = exec.Command("sudo", "netstat", flag)
}
buf := new(bytes.Buffer)
cmd.Stdout = buf
cmd.Stderr = buf
if err := cmd.Run(); err != nil {
return nil, err
}
rd := bufio.NewReader(buf)
lines := [][]string{}
for {
l, _, err := rd.ReadLine()
if err == io.EOF {
break
}
s := string(l)
if !strings.HasPrefix(s, socket) {
if !strings.HasPrefix(s, "tcp") && socket != "tcp*" {
continue
}
}
sl := strings.Fields(s)
lines = append(lines, sl)
}
socketIdx := 0
portIdx := 3
programIdx := 6
ps := []Process{}
fmt.Fprintf(w, "[NetStat] 'netstat %s' returned %d lines.\n", flag, len(lines))
for _, sl := range lines {
theSocket := sl[socketIdx]
if theSocket != socket {
if !strings.HasPrefix(theSocket, "tcp") && socket != "tcp*" {
fmt.Fprintln(w, "[NetStat] different socket. Skipping", sl)
continue
}
}
asl := strings.Split(sl[portIdx], ":")
if len(asl) < 2 {
fmt.Fprintln(w, "[NetStat] skipping", sl)
continue
}
thePort := ":" + asl[len(asl)-1]
if port != "*" {
if thePort != port {
fmt.Fprintln(w, "[NetStat] different port. Skipping", sl)
continue
}
}
theHost := strings.TrimSpace(strings.Replace(sl[portIdx], thePort, "", -1))
psl := strings.SplitN(sl[programIdx], "/", 2)
if len(psl) != 2 {
continue
}
theProgram := strings.TrimSpace(psl[1])
if program != "*" {
if theProgram != program {
fmt.Fprintln(w, "[NetStat] different program. Skipping", sl)
continue
}
}
thePID := 0
if d, err := strconv.Atoi(psl[0]); err != nil {
fmt.Fprintln(w, "[NetStat - error] %v / Skipping %+v\n", err, sl)
continue
} else {
thePID = d
}
p := Process{}
p.Socket = theSocket
p.Program = theProgram
p.Host = theHost
p.Port = thePort
p.PID = thePID
ps = append(ps, p)
}
for _, v := range ps {
fmt.Fprintf(w, "[NetStat] %s\n", v)
}
return ps, nil
}