-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharcs.go
55 lines (44 loc) · 1.33 KB
/
arcs.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
package main
import (
"fmt"
"os/exec"
"syscall"
)
func execWindows(ports, portRange []string) (string, error) {
var ws syscall.WaitStatus
for _, port := range ports {
command := fmt.Sprintf("(Get-NetTCPConnection -LocalPort %s).OwningProcess -Force", port)
cmd := exec.Command("Stop-Process", "-Id", command)
if err := cmd.Run(); err != nil {
if err != nil {
return "", err
}
if exiterr, ok := err.(*exec.ExitError); ok {
ws = exiterr.Sys().(syscall.WaitStatus)
return "", exiterr
}
}
ws = cmd.ProcessState.Sys().(syscall.WaitStatus)
fmt.Printf("Port successfully killed (exit code: %s)\n", []byte(fmt.Sprintf("%d", ws.ExitStatus())))
}
return "complete", nil
}
func execUnix(ports, portRange []string) (string, error) {
var ws syscall.WaitStatus
for _, port := range ports {
command := fmt.Sprintf("lsof -i tcp:%s | grep LISTEN | awk '{print $2}' | xargs kill -9", port)
cmd := exec.Command("bash", "-c", command)
if err := cmd.Run(); err != nil {
if err != nil {
return "", err
}
if exiterr, ok := err.(*exec.ExitError); ok {
ws = exiterr.Sys().(syscall.WaitStatus)
return "", exiterr
}
}
ws = cmd.ProcessState.Sys().(syscall.WaitStatus)
fmt.Printf("Port successfully killed (exit code: %s)\n", []byte(fmt.Sprintf("%d", ws.ExitStatus())))
}
return "complete", nil
}