【问题标题】:stdout/stderr - ValueError: too many values to unpack (expected 2)stdout/stderr - ValueError: 要解压的值太多(预期为 2)
【发布时间】:2018-06-27 02:29:24
【问题描述】:

我有一个脚本在 python 2.7 中运行良好,但现在在 3.6 中它失败了。特别是我此时遇到了问题

下面的代码行正在调用下面的函数:

exec_command(ssh, 'systemctl status myservice', timeout=60)

然后执行函数。在这种情况下,ssh 是使用 paramiko 的 ssh 连接。

def exec_command(ssh, cmd, timeout, want_exitcode=False):
    # one channel per command
    stdout, stderr = ssh.exec_command(cmd)
    # get the shared channel for stdout/stderr
    channel = stdout.channel

    # indicate that we're not going to write to that channel anymore
    channel.shutdown_write()

    # read stdout/stderr in order to prevent read block hangs
    stdout_chunks = []
    stdout_chunks.append(stdout.channel.recv(len(stdout.channel.in_buffer)))

运行时出现以下错误:

文件“path\myscript.py”,第 54 行,在 exec_command ValueError:要解包的值太多(预期为 2) [13340] 执行脚本 myscript 失败

第 54 行是stdout, stderr = ssh.exec_command(cmd)

【问题讨论】:

  • 您应该执行print(ssh.exec_command(cmd)) 以了解返回了多少值。然后找出你感兴趣的。

标签: python python-3.x


【解决方案1】:

您从未说过 ssh.exec_command 是什么,但我假设您正在使用 paramiko 访问某个 ssh 服务器。

正如您在 http://docs.paramiko.org/en/2.4/api/client.html#paramiko.client.SSHClient.exec_command 的文档中看到的那样,此函数返回 3 个值,而不是 2...所以您应该将行更改为:

stdin, stdout, stderr = ssh.exec_command(cmd)

现在我不确定为什么它以前可以工作。也许 paramiko api 改变了?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 2019-04-19
    • 2014-07-31
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多