【问题标题】:Get socket and pipe name in CentOS [closed]在 CentOS 中获取套接字和管道名称 [关闭]
【发布时间】:2018-12-23 19:19:22
【问题描述】:

基本上,我想使用“stat”命令从位于 /proc/PID/fd/ 目录中的 PID 开始获取套接字和管道所有者:

lrwx------ 1 root root 64 dic 23 17:52 0 -> socket:[9790]
l-wx------ 1 root root 64 dic 23 17:52 1 -> /var/log/messages
l-wx------ 1 root root 64 dic 23 17:52 2 -> /var/log/secure
lr-x------ 1 root root 64 dic 23 17:52 3 -> /proc/kmsg
l-wx------ 1 root root 64 dic 23 17:52 4 -> /var/log/maillog
l-wx------ 1 root root 64 dic 23 17:52 5 -> /var/log/cron

当我只有套接字名称 (socket:[9790]) 而不是其符号引用 (0,1,2...) 时,如何使用“stat”来获取每个套接字所有者?

感谢您的回答!

【问题讨论】:

  • StackOverflow 的范围仅限于编写代码Unix & LinuxSuperUser 更适合解决有关使用标准 UNIX 工具的问题。

标签: bash sockets pipe proc stat


【解决方案1】:

为此lsof 要好得多。

您可以使用lsof -i -a -p $PID

StackExchange 上的 unix hub 上有一个类似的问题。

https://unix.stackexchange.com/questions/235979/how-do-i-find-out-more-about-socket-files-in-proc-fd

我认为这应该可以解决您的问题。

【讨论】:

    【解决方案2】:

    我不认为你可以通过一个简单的stat 指令来做到这一点。相反,您可以尝试使用find 来搜索符号链接,然后将它们格式化打印。然后,您可以将结果通过管道传输到 grepcut 以过滤您需要的信息:

    find /proc/PID/fd/ -type l -printf '%u %l\n' 2>/dev/null|grep "socket:[9790]"|cut -d' ' -f1
    

    您可以使用find 的选项(-mindepth-maxdepth)来确定它应该在多深的子文件夹中进行搜索。如果您想要所有者的 UID 而不是用户名,请在 -printf 中使用 %U 而不是 %u

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      相关资源
      最近更新 更多