Last active
September 6, 2016 00:17
-
-
Save theothertomelliott/de547a6eef7f9ce27818e2fee19224a4 to your computer and use it in GitHub Desktop.
Get a list of ports open on the current host
This file contains hidden or 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
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