【问题标题】:No output when executing smartctl commands over SSH using Paramiko (Python) [duplicate]使用Paramiko(Python)通过SSH执行smartctl命令时没有输出[重复]
【发布时间】:2020-07-21 11:11:15
【问题描述】:

我正在使用以下代码通过 SSH 成功执行命令:

import paramiko

hosts = ["192.168.1.156"]

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

for host in hosts:
    client.connect(host, username='user', password='******')
    stdin, stdout, stderr = client.exec_command("df -H")
    output = ''.join(stdout.readlines())

Print(output)

但是,一旦我将“df -H”命令换成“smartctl -H disk1”,Python 就没有任何输出。 值得一提的是,我也没有收到任何错误。

当我在终端中运行“smartctl -H disk1”时,它工作正常并给出了我期望的输出,但它只是通过似乎是问题的 Paramiko 命令运行它。

有什么想法吗?

干杯,

乔治

【问题讨论】:

  • “我也没有收到任何错误” – 你的意思是在 stderr 上没有输出吗?或者你没有得到任何 Python 错误? + 你见过Command executed with Paramiko does not produce any output吗?
  • 感谢您的回复,使用不同的命令(例如“df -H”或“mount”)输出工作正常,但一旦使用“smartctl”,就没有输出。感谢您的链接,它实际上不是答案,但该答案中的链接解决了我的问题。
  • 没想到这是一回事,谢谢你告诉我。

标签: python bash ssh paramiko


【解决方案1】:

取决于哪个命令不起作用。

client.exec_command("smartctl -H disk1")

然后我将“which smartctl”输入到一个终端,它给出了“/usr/local/bin/smartctl”

替换它而不只是 smartctl,效果很好。

如client.exec_command("/usr/local/bin/smartctl -H disk1")

干杯!

【讨论】: