Difference between revisions of "Golnag killing port pid"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
+ | ``` | ||
package main | package main | ||
Line 66: | Line 67: | ||
} | } | ||
} | } | ||
+ | ``` |
Latest revision as of 00:56, 12 March 2022
package main import ( "fmt" "github.com/drael/GOnetstat" "os" "strconv" // "syscall" // "errors" ) func main() { // listports() killPort(9999) } func stopServer(proc *os.Process) { err := proc.Kill() if err != nil { panic(err.Error()) } proc.Release() } func killPID(pid int) { process, err := os.FindProcess(pid) if err != nil { panic(err.Error()) } process.Signal(os.Interrupt) } func killPort(port int64) { d := GOnetstat.Tcp() for _, p := range d { if p.State == "LISTEN" && p.Port == port { fmt.Printf("Killing port, pid, name %v %v %v\n", port, p.Pid, p.Name) pid, err := strconv.Atoi(p.Pid) if err != nil { panic(err.Error()) } killPID(pid) } } } func listPorts() { d := GOnetstat.Tcp() fmt.Printf("Proto %16s %20s %14s %14s %14s\n", "Local Adress", "Foregin Adress", "State", "Pid", "Program") for _, p := range d { if p.State == "LISTEN" { fmt.Printf("foo") } if p.State == "LISTEN" { ip_port := fmt.Sprintf("%v:%v", p.Ip, p.Port) fip_port := fmt.Sprintf("%v:%v", p.ForeignIp, p.ForeignPort) pid := fmt.Sprintf("%v", p.Pid) program := fmt.Sprintf("%v", p.Name) fmt.Printf("tcp %16v %20v %16v %14v %14v\n", ip_port, fip_port, p.State, pid, program) } } }