您可以使用lsof
只要连接上存在活动流量,即可检测谁正在使用该连接。
这是一个演示:
- 在给定端口上设置服务器失败并出现错误地址已被使用
-
lsof https://en.wikipedia.org/wiki/Lsof不报告该端口的任何侦听器
下面是 shell 日志,演示了这一点:
python -m SimpleHTTPServer 3333 2>&1 | fgrep error
Output:
socket.error: [Errno 48] Address already in use
sudo lsof -i TCP:3333
echo $?
Output:
1
[1]
:在端口上启动网络服务器3333
因错误而失败Address already in use
[2]
: lsof
不报告端口3333
被任何人利用
让我们产生流量来强制lsof
检测端口的使用情况:在另一个终端中打开telnet
联系:
telnet localhost 3333
现在回到你之前的终端,你会看到lsof
找到你的端口:
sudo lsof -n -P -i :3333
Output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
telnet 78142 loic 5u IPv4 0x3fa2e8474ece6129 0t0 TCP 127.0.0.1:51855->127.0.0.1:3333 (ESTABLISHED)
There is traffic going on, but according to the OS, only one end of the connection is there, the initiator, there still isn’t any `LISTENER`!
注意:就我而言,操作系统是macOS v10.13.3 https://en.wikipedia.org/wiki/MacOS_High_Sierra(High Sierra),但我在以前的 macOS/OSX 版本中也遇到过这种情况