我发现命令的输出中使用了 Unix 套接字lsof:
COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME
screen 110970 username 4u unix 0xffff91fe3134c400 0t0 19075659 socket
Run Code Online (Sandbox Code Playgroud)
“DEVICE”列保存看起来像内存地址的内容。根据 lsof 手册页:
DEVICE contains the device numbers, separated by commas, for a character special, block special, regular, directory or NFS file;
or ``memory'' for a memory file system node under Tru64 UNIX;
or the address of the private data area of a Solaris socket stream;
or a kernel reference address that identifies the file (The kernel reference address may be used for FIFO's, for example.);
or the base address or device name of a Linux AX.25 socket device.
Usually only the lower thirty two bits of Tru64 UNIX kernel addresses are displayed.
Run Code Online (Sandbox Code Playgroud)
我的问题是,我正在寻找其中哪些具有价值0xffff91fe3134c400?
另外,我如何与其互动?我知道我可以用来netcat连接到 Unix 域套接字,但从在线阅读示例来看,您似乎必须指定一个文件。
要查找与 UNIX 套接字关联的文件,可以使用 标志+E来lsof显示套接字的端点。从手册页:
+|-E +E 指定 Linux 管道、Linux UNIX 套接字和 Linux 伪终端文件应与端点信息一起显示,并且还应显示端点的文件
例如,以下是某人试图找出进程 fd 6 的端点时提出的问题top中的一些示例:
# lsof -d 6 -U -a +E -p $(pgrep top)
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dbus-daem 874 messagebus 12u unix 0xffff9545f6fee400 0t0 366381191 /var/run/dbus/system_bus_socket type=STREAM ->INO=366379599 25127,top,6u
top 25127 root 6u unix 0xffff9545f6fefc00 0t0 366379599 type=STREAM ->INO=366381191 874,dbus-daem,12u
Run Code Online (Sandbox Code Playgroud)
标志仅-U显示lsofUnix 套接字文件。
请注意,您只会看到侦听进程的套接字文件的名称。另一个进程不会显示unix套接字文件的名称,但使用+Elsof将显示侦听套接字文件的inode,并且还会为侦听此套接字的进程添加一行(以及套接字文件名)。
在这个例子中请注意,我们只要求lsof显示命令的文件描述符top,但lsof添加了另一行dbus-daem- 这是监听进程,它监听的套接字文件是/var/run/dbus/system_bus_socket.
type=STREAM ->INO=366381191 874,dbus-daem,12u)交互/var/run/dbus/system_bus_socket type=STREAM ->INO=366379599 25127,top,6u),在那里你可以看到套接字文件名为/var/run/dbus/system_bus_socket.另外,我如何与其互动?
现在您已经有了 UNIX 套接字的文件名,您可以通过多种方式与其交互,例如:
socat - UNIX-CONNECT:/run/dbus/system_bus_socket
nc -U /run/dbus/system_bus_socket
Run Code Online (Sandbox Code Playgroud)
有关更多信息: 如何通过 Debian Squeeze 上的 shell 与 Unix 域套接字通信?