├── README.md ├── go.mod ├── go.sum └── main.go /README.md: -------------------------------------------------------------------------------- 1 | # netstatgo 2 | 3 | > John Hammond | Monday, April 10, 2023 4 | 5 | --------------- 6 | 7 | Crappy Golang code to list local listening ports and their associated processes, much like `netstat -anob` on Windows. 8 | 9 | To receive all possible information, run as an administrator. 10 | 11 | ## Sample Output 12 | 13 | ``` 14 | Proto Local Address Foreign Address State PID/Name 15 | TCP 0.0.0.0:135 0.0.0.0:0 LISTEN 1948 /svchost.exe 16 | TCP 0.0.0.0:445 0.0.0.0:0 LISTEN 4 /System 17 | TCP 0.0.0.0:903 0.0.0.0:0 LISTEN 6092 /vmware-authd.exe 18 | TCP 0.0.0.0:913 0.0.0.0:0 LISTEN 6092 /vmware-authd.exe 19 | TCP 0.0.0.0:1824 0.0.0.0:0 LISTEN 18144 /WaveLink.exe 20 | TCP 0.0.0.0:3389 0.0.0.0:0 LISTEN 1220 /svchost.exe 21 | TCP 0.0.0.0:5040 0.0.0.0:0 LISTEN 11008 /svchost.exe 22 | TCP 0.0.0.0:7680 0.0.0.0:0 LISTEN 10192 /svchost.exe 23 | TCP 0.0.0.0:49664 0.0.0.0:0 LISTEN 1632 /lsass.exe 24 | TCP 0.0.0.0:49665 0.0.0.0:0 LISTEN 1536 /wininit.exe 25 | TCP 0.0.0.0:49666 0.0.0.0:0 LISTEN 1052 /svchost.exe 26 | TCP 0.0.0.0:49667 0.0.0.0:0 LISTEN 2908 /svchost.exe 27 | TCP 0.0.0.0:49668 0.0.0.0:0 LISTEN 3104 /svchost.exe 28 | TCP 0.0.0.0:49669 0.0.0.0:0 LISTEN 4884 /spoolsv.exe 29 | TCP 0.0.0.0:49671 0.0.0.0:0 LISTEN 1612 /services.exe 30 | TCP 127.0.0.1:28198 0.0.0.0:0 LISTEN 18144 /WaveLink.exe 31 | TCP 127.0.0.1:37373 0.0.0.0:0 LISTEN 32188 /gocode.exe 32 | TCP 127.0.0.1:49670 0.0.0.0:0 LISTEN 5512 /dirmngr.exe 33 | TCP 127.0.0.1:50796 0.0.0.0:0 LISTEN 5212 /dbus-daemon.exe 34 | TCP 127.0.0.1:50796 0.0.0.0:0 LISTEN 5212 /dbus-daemon.exe 35 | TCP 127.0.0.1:56993 127.0.0.1:56994 ESTABLISHED 27928 /Zoom.exe 36 | TCP 127.0.0.1:56994 127.0.0.1:56993 ESTABLISHED 27928 /Zoom.exe 37 | TCP 127.0.0.1:58582 127.0.0.1:58583 ESTABLISHED 11252 /vmware.exe 38 | TCP 127.0.0.1:58583 127.0.0.1:58582 ESTABLISHED 11252 /vmware.exe 39 | TCP 127.0.0.1:58586 127.0.0.1:58587 ESTABLISHED 11252 /vmware.exe 40 | TCP 127.0.0.1:58587 127.0.0.1:58586 ESTABLISHED 11252 /vmware.exe 41 | TCP 127.0.0.1:58588 127.0.0.1:58589 ESTABLISHED 11252 /vmware.exe 42 | TCP 127.0.0.1:58589 127.0.0.1:58588 ESTABLISHED 11252 /vmware.exe 43 | ``` -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/JohnHammond/netstatgo 2 | 3 | go 1.18 4 | 5 | require github.com/shirou/gopsutil v3.21.11+incompatible 6 | 7 | require ( 8 | github.com/go-ole/go-ole v1.2.6 // indirect 9 | github.com/stretchr/testify v1.8.2 // indirect 10 | github.com/tklauser/go-sysconf v0.3.11 // indirect 11 | github.com/tklauser/numcpus v0.6.0 // indirect 12 | github.com/yusufpapurcu/wmi v1.2.2 // indirect 13 | golang.org/x/sys v0.7.0 // indirect 14 | ) 15 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= 5 | github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= 6 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 7 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 8 | github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= 9 | github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= 10 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 11 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 12 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 13 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 14 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 15 | github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= 16 | github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 17 | github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM= 18 | github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI= 19 | github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms= 20 | github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4= 21 | github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= 22 | github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= 23 | golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 24 | golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 25 | golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= 26 | golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 27 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 28 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 29 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 30 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 31 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/shirou/gopsutil/net" 6 | "github.com/shirou/gopsutil/process" 7 | "log" 8 | "strconv" 9 | "strings" 10 | "syscall" 11 | ) 12 | 13 | func protocolToString(connType uint32, ip string) string { 14 | isIPv6 := strings.Contains(ip, ":") 15 | switch connType { 16 | case syscall.SOCK_STREAM: // TCP 17 | if isIPv6 { 18 | return "TCP6" 19 | } 20 | return "TCP" 21 | case syscall.SOCK_DGRAM: // UDP 22 | if isIPv6 { 23 | return "UDP6" 24 | } 25 | return "UDP" 26 | default: 27 | return "Unknown" 28 | } 29 | } 30 | 31 | func main() { 32 | // Get network connections 33 | conns, err := net.Connections("all") 34 | if err != nil { 35 | log.Fatal(err) 36 | } 37 | 38 | fmt.Printf("%-7s %-25s %-25s %-10s %-10s\n", "Proto", "Local Address", "Foreign Address", "State", "PID/Name") 39 | 40 | for _, conn := range conns { 41 | // Filter out connections with no associated process 42 | if conn.Pid == 0 { 43 | continue 44 | } 45 | 46 | // Get process name 47 | proc, err := process.NewProcess(conn.Pid) 48 | if err != nil { 49 | log.Println(err) 50 | continue 51 | } 52 | procName, err := proc.Name() 53 | if err != nil { 54 | log.Println(err) 55 | continue 56 | } 57 | 58 | // Convert the protocol to a string 59 | protocol := protocolToString(conn.Type, conn.Laddr.IP) 60 | 61 | // Print connection details 62 | localAddr := conn.Laddr.IP + ":" + strconv.Itoa(int(conn.Laddr.Port)) 63 | remoteAddr := conn.Raddr.IP + ":" + strconv.Itoa(int(conn.Raddr.Port)) 64 | fmt.Printf("%-7s %-25s %-25s %-10s %-10d/%s\n", protocol, localAddr, remoteAddr, conn.Status, conn.Pid, procName) 65 | } 66 | } 67 | --------------------------------------------------------------------------------