使用Mac OS X上的进程IDloggingnetworkingstream量?

我想logging在Mac OS X服务器上的所有networkingstream量(如tcpdump一样),但包括负责的stream程的ID。 使用lsof只会给我当前的连接,而不是过去的连接。

有没有办法做到这一点?

问候,乔臣

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

  -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. 

所以例如显示进程ID和进程名称,你可以这样做:

 tcpdump -k NP 

我最近也有这个问题。 你可能采取的一种方法是同时使用tcpdump和nettop (每个并排,在他们自己的terminalshell中),并作为networkingstream量清单进行可视化观察。

例如,我的防火墙最近发现了一个可疑的networkingstream量,这个networkingstream量是从我的Mac的以太网出来的,在一个我不认识的私有子网上,以及一个看起来不寻常的端口(TCP 7000)。 在一个terminalshell我跑:

 $ sudo tcpdump port 7000 

并在另一个terminalshell我跑:

 $ sudo nettop -m tcp 

当networkingstream量通过tcpdump显示时,它几乎立即被添加到nettop输出,这表明它是苹果公司的AirPlay助手守护进程运行的进程ID(PID)87在这个例子中:

 AirPlayXPCHelpe.87 

对于观察networking中看起来反常的东西的所有情况,这可能不一定是实际的,但是作为一个起点可能值得尝试,也可能写一个更复杂的DTrace脚本。

不幸的是,最好的方式( tcpsnoop ,它使用DTrace来完成你所描述的)不适用于dtrace的Mac OS X实现。 您需要某种应用程序防火墙(例如Little Snitch ),但这更像是桌面/单一用户的事情,而不是您希望在服务器上运行的东西。

这实际上似乎是一个奇怪的要求,你是想做一个入侵检测系统,或只是找出是什么导致networking访问?