Skip to content

Instantly share code, notes, and snippets.

@theothertomelliott
Last active September 6, 2016 00:17
Show Gist options
  • Save theothertomelliott/de547a6eef7f9ce27818e2fee19224a4 to your computer and use it in GitHub Desktop.
Save theothertomelliott/de547a6eef7f9ce27818e2fee19224a4 to your computer and use it in GitHub Desktop.
Get a list of ports open on the current host
package main
import (
"fmt"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/process"
)
func main() {
connections, err := net.Connections("all")
if err != nil {
panic(err)
}
for _, connection := range connections {
if connection.Status == "LISTEN" {
fmt.Printf("%v: ", connection.Laddr.Port)
p, err := process.NewProcess(connection.Pid)
if err != nil {
fmt.Println(err)
continue
}
cmdline, err := p.Cmdline()
if err != nil {
fmt.Println(err)
continue
}
fmt.Printf("%v\n", cmdline)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment