Difference between revisions of "Golnag killing port pid"
Jump to navigation
Jump to search
(Created page with "``` package main import ( "fmt" "github.com/drael/GOnetstat" "os" "strconv" // "syscall" // "errors" ) func main() { // listports...") |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 3: | Line 3: | ||
import ( | import ( | ||
| − | + | "fmt" | |
| − | + | "github.com/drael/GOnetstat" | |
"os" | "os" | ||
"strconv" | "strconv" | ||
| Line 12: | Line 12: | ||
func main() { | func main() { | ||
| − | + | // listports() | |
killPort(9999) | killPort(9999) | ||
} | } | ||
| Line 19: | Line 19: | ||
err := proc.Kill() | err := proc.Kill() | ||
if err != nil { | if err != nil { | ||
| − | + | panic(err.Error()) | |
} | } | ||
proc.Release() | proc.Release() | ||
} | } | ||
| − | + | func killPID(pid int) { | |
| − | func killPID(pid int){ | + | process, err := os.FindProcess(pid) |
| − | process, err := os.FindProcess(pid) | ||
if err != nil { | if err != nil { | ||
panic(err.Error()) | panic(err.Error()) | ||
| Line 33: | Line 32: | ||
} | } | ||
| − | func killPort(port int64){ | + | 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() { | func listPorts() { | ||
| − | + | d := GOnetstat.Tcp() | |
| − | + | fmt.Printf("Proto %16s %20s %14s %14s %14s\n", "Local Adress", "Foregin Adress", | |
"State", "Pid", "Program") | "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) | p.State, pid, program) | ||
| − | + | } | |
| − | + | } | |
} | } | ||
``` | ``` | ||
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)
}
}
}