I want to log all network traffic on a Mac OS X server (like tcpdump does), but including the ID of the process that is responsible. Using lsof only gives me the current connections, not the past ones.
Is there a way to do this?
I recently had this problem too. One approach you can potentially take is to use both tcpdump and nettop in parallel (each, side by side, in their own terminal shells), and visually observe as the network traffic manifests.
For example, my firewall recently caught suspicious looking network traffic coming out of my Mac's Ethernet, on a private subnet which I did not recognize and on a port (TCP 7000) which looked unusual. In one terminal shell I ran:
and in the other terminal shell I ran:
When the network traffic showed up via tcpdump, it nearly instantly was also was added to the nettop output, which indicated it was Apple's AirPlay helper daemon running with process ID (PID) 87 in this example:
This may not necessarily be practical for observing all situations where you're trying to catch something on your network which looks anomalous but as a starting point may be worth trying vs writing a probably more complex DTrace script.
Sadly, the best way to do this (tcpsnoop, which uses DTrace to do exactly what you describe) does not work on the Mac OS X implementation of dtrace. You'd need some kind of application firewall (Little Snitch for example) but that's more of a desktop / single user thing than something you want running on your server.
This seems like an odd requirement actually, are you trying to make an intrusion detection system, or just figure out what is causing network access?