-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from kubescape/rem-netstat
remove netstat dependency
- Loading branch information
Showing
3 changed files
with
47 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,69 @@ | ||
package test | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/cakturk/go-netstat/netstat" | ||
) | ||
|
||
const windows = "windows" | ||
|
||
func killPortProcess(targetPort int) error { | ||
socks6, err := netstat.TCP6Socks(netstat.NoopFilter) | ||
if err != nil { | ||
return err | ||
} | ||
socks, err := netstat.TCPSocks(netstat.NoopFilter) | ||
processes, err := getProcessesForPort(targetPort) | ||
if err != nil { | ||
return err | ||
} | ||
for _, sock := range append(socks6, socks...) { | ||
if sock.LocalAddr.Port == uint16(targetPort) { | ||
if sock.Process == nil { | ||
continue | ||
|
||
for _, pid := range processes { | ||
fmt.Printf("Killing process on port %d with PID %d\n", targetPort, pid) | ||
|
||
switch runtime.GOOS { | ||
case windows: | ||
killCmd := exec.Command("taskkill", "/F", "/PID", fmt.Sprint(pid)) | ||
if err := killCmd.Run(); err != nil { | ||
return err | ||
} | ||
pid := sock.Process.Pid | ||
default: | ||
process, err := os.FindProcess(pid) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Killing process of port", pid, targetPort) | ||
|
||
// Send a SIGTERM signal to the process | ||
err = process.Signal(syscall.SIGTERM) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getProcessesForPort(targetPort int) ([]int, error) { | ||
var cmd *exec.Cmd | ||
processes := make([]int, 0) | ||
switch runtime.GOOS { | ||
case windows: | ||
cmd = exec.Command("cmd", "/c", "netstat", "-ano", "|", "findstr", fmt.Sprintf(":%d", targetPort)) | ||
default: | ||
cmd = exec.Command("sh", "-c", fmt.Sprintf("lsof -i tcp:%d | awk 'NR!=1 {print $2}'", targetPort)) | ||
} | ||
output, err := cmd.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
scanner := bufio.NewScanner(strings.NewReader(string(output))) | ||
for scanner.Scan() { | ||
pidStr := scanner.Text() | ||
pidStr = strings.TrimSpace(strings.Split(pidStr, " ")[0]) // Extracting PID from the output | ||
|
||
pid, err := strconv.Atoi(pidStr) | ||
if err != nil { | ||
continue // Not a valid number, skip | ||
} | ||
processes = append(processes, pid) | ||
} | ||
|
||
return processes, scanner.Err() | ||
} |