【问题标题】:How to use the lsof command using pssh/fabric library? Bash: lsof command not found [duplicate]如何通过 pssh/fabric 库使用 lsof 命令? Bash:找不到lsof命令[重复]
【发布时间】:2019-12-19 21:58:27
【问题描述】:

Lsof: 连接远程服务器时找不到 Bash 命令。在终端上也可以完美运行。

我尝试在 python 中使用以下三个用于 ssh 的库:pssh、fabric 和 paramiko。

主要错误是使用 pssh 和 fabric 库,我的大多数命令都可以工作,即 ls、df、kill 等。但是 lsof 命令不起作用。它会抛出此错误“lsof:找不到 bash 命令”。如果我使用 putty 连接到终端并运行相同的命令,Lsof 可以完美运行。

所以,我尝试了 paramiko 的 invoke_shell 变体,因为它使用了 shell 通道。虽然 paramiko 的 invoke_shell 正确运行 lsof 命令,但它很耗时。

面料代码:

c = Connection()
result = c.run('lsof /lt')

print(result.stdout.strip())

Pssh 代码:

from __future__ import print_function

from pssh.clients import ParallelSSHClient

hosts = ['host']
client = ParallelSSHClient(hosts, user='usename', password='pass')

output = client.run_command('lsof /lt | grep deleted ',  sudo=True)
for host, host_output in output.items():
    for line in host_output.stdout:
        print(line)

由于我对此很陌生,因此非常感谢任何帮助。请让我知道解决此问题的最佳方法是什么。

【问题讨论】:

  • 远程服务器上lsof命令的路径是什么?如果您使用此二进制文件的完全限定路径(例如,/usr/local/bin/lsof)而不是非限定名称(lsof)会发生什么?

标签: ssh paramiko fabric lsof pssh


【解决方案1】:

感谢@larsks 的评论。这样就解决了。

我所做的是: 运行 type -a lsof 并从那里复制路径,然后将其添加到我的脚本中。

非常感谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    相关资源
    最近更新 更多