Skip to content

Commit

Permalink
Merge pull request #15 from kubescape/rem-netstat
Browse files Browse the repository at this point in the history
remove netstat dependency
  • Loading branch information
refaelm92 authored Nov 13, 2023
2 parents c57feff + 2c5469a commit c688c98
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.18
require (
github.com/apache/pulsar-client-go v0.9.0
github.com/avast/retry-go v3.0.0+incompatible
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/otel v1.13.0
go.opentelemetry.io/otel/trace v1.13.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b/go.mod h1:ac9efd0D1fsDb3EJvhqgXRbFx7bs2wqZ10HQPeU8U/Q=
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5 h1:BjkPE3785EwPhhyuFkbINB+2a1xATwk8SNDWnJiD41g=
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5/go.mod h1:jtAfVaU/2cu1+wdSRPWE2c1N2qeAA3K4RH9pYgqwets=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
Expand Down
66 changes: 47 additions & 19 deletions pulsar/test/kill_port_process.go
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()
}

0 comments on commit c688c98

Please sign in to comment.