by woodruffw · 2y ago · 0 comments
(Source: I worked on osquery's core and tables for 2-3 years for various clients, and my company employs several of the current maintainers.)
sockets() {
(osqueryi --list --separator ',' | column -t -s ',') <<EOF
SELECT s.local_port, p.cmdline
FROM process_open_sockets AS s
INNER JOIN processes AS p
ON s.pid = p.pid
WHERE s.state = 'LISTEN'
ORDER BY p.cmdline;
EOF
}(In terms of general purpose/flexible tooling, I'm not aware of a close replacement for osquery.)
shouldn't lsof with some awk munging get you there ?
edit-01:
for example
lsof -iTCP -sTCP:LISTEN -n -P | awk '{print $1, substr($0, index($0,$9))}'
might be useful enough ? fwiw, it does give me something useful on my machine alias ports="lsof -iTCP -sTCP:LISTEN -n -P | awk '{print \$1, substr(\$0, index(\$0,\$9))}'"