在 Mac OS X 上使用进程 ID 记录网络流量?

Joc*_*hen 5 mac-osx lsof tcpdump

我想记录 Mac OS X 服务器上的所有网络流量(就像 tcpdump 那样),但包括负责进程的 ID。使用 lsof 只给我当前的连接,而不是过去的连接。

有没有办法做到这一点?

问候, 乔亨

小智 9

虽然nettop可以很好地查看当前连接的快照,但您也可以使用tcpdump来收集数据和处理信息。随着tcpdump使用的选项-k来显示的元数据捕获的流量。

   -k     Control the display of packet metadata via an optional metadata_arg argument. This is useful when displaying packet saved
          in the pcap-ng file format or with interfaces that support the PKTAP data link type.

          By default, when the metadata_arg optional argument is not  specified,  any  available  packet  metadata  information  is
          printed out.

          The  metadata_arg  argument  controls  the  display of specific packet metadata information using a flag word, where each
          character corresponds to a type of packet metadata as follows:

                 I     interface name (or interface ID)
                 N     process name
                 P     process ID
                 S     service class
                 D     direction
                 C     comment

          This is an Apple modification.
Run Code Online (Sandbox Code Playgroud)

因此,例如显示进程 ID 和进程名称,您可以这样做:

tcpdump -k NP
Run Code Online (Sandbox Code Playgroud)