This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (95 loc) · 2.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/tabwriter"
)
type NetworkConnection struct {
Proto string
LocalIP string
LocalPort string
RemoteIP string
RemotePort string
ProgramCommand string
}
func listNetworkConnections() ([]NetworkConnection, error) {
// `lsof` to get network connections
cmd := exec.Command("lsof", "-i", "-P", "-n", "-F", "cna")
output, err := cmd.Output()
if err != nil {
return nil, err
}
// read the output line by line
scanner := bufio.NewScanner(strings.NewReader(string(output)))
scanner.Split(bufio.ScanLines)
var connections []NetworkConnection
var connection NetworkConnection
for scanner.Scan() {
line := scanner.Text()
switch {
case strings.HasPrefix(line, "p"):
connection = NetworkConnection{}
connection.ProgramCommand = strings.TrimPrefix(line, "p")
case strings.HasPrefix(line, "c"):
connection.Proto = strings.TrimPrefix(line, "c")
case strings.HasPrefix(line, "n"):
addr := strings.TrimPrefix(line, "n")
parts := strings.Split(addr, "->")
if len(parts) != 2 {
continue
}
localAddr := strings.Split(parts[0], ":")
remoteAddr := strings.Split(parts[1], ":")
if len(localAddr) != 2 || len(remoteAddr) != 2 {
continue
}
// assign the local and remote ip:port info
connection.LocalIP = localAddr[0]
connection.LocalPort = localAddr[1]
connection.RemoteIP = remoteAddr[0]
connection.RemotePort = remoteAddr[1]
connections = append(connections, connection)
}
}
// get the program name for this pid
for i := range connections {
connections[i].ProgramCommand, err = getBinaryName(connections[i].ProgramCommand)
if err != nil {
return nil, err
}
}
return connections, nil
}
func getBinaryName(pid string) (string, error) {
// use ps to cross reference the pid
cmd := exec.Command("ps", "-p", pid, "-o", "comm=")
output, err := cmd.Output()
if err != nil {
return "", err
}
// clean it up
binaryName := strings.TrimSpace(string(output))
return filepath.Base(binaryName), nil
}
func displayNetworkConnections() {
connections, err := listNetworkConnections()
if err != nil {
fmt.Println("Error:", err)
return
}
// try our best to align the otuput
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "Proto\t\t\t\tLocal IP\tLocal Port\tRemote IP\tRemote Port\tBinary Name")
fmt.Fprintln(w, "-----------------------------------------------------------------------------")
for _, conn := range connections {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", conn.Proto, conn.LocalIP, conn.LocalPort, conn.RemoteIP, conn.RemotePort, conn.ProgramCommand)
}
w.Flush()
}
func main() {
displayNetworkConnections()
}