【问题标题】:Paramiko exec_command fails with 'NoneType' object is not iterableParamiko exec_command 失败,“NoneType”对象不可迭代
【发布时间】:2018-06-29 14:24:11
【问题描述】:

我是帕拉米科的新手。我正在尝试创建一个简单的脚本,允许任何人使用他们的 Linux 凭据来运行命令。我决定使用简单的ls 命令进行测试,但收到错误消息。

import paramiko
username = *<USERNAME>*
hostname = *<HOSTNAME>*
port = 22
trans = paramiko.Transport((hostname,port))
trans.connect(username=username, password=password)
channel = trans.open_channel("session")
print(channel.send_ready())
print(channel.get_transport())
stdin,stdout,stderr = channel.exec_command("ls -lah")
trans.close()

我收到以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-28-ce837beea6fe> in <module>()
      6 trans.connect(username=username, password=password)
      7 channel = trans.open_channel("session")
----> 8 stdin,stdout,stderr = channel.exec_command("ls -lah")

TypeError: 'NoneType' object is not iterable

关于我可能做错了什么有什么想法吗?

【问题讨论】:

  • 您在the documentation 的什么地方看到exec_command 返回标准输入、标准输出和标准错误的元组?相反,它表示命令的 I/O 已连接到通道。显然,它返回 None.
  • @MoxieBall 但是 OP 调用 Channel.exec_command,而不是 SSHClient.exec_command - 实际上是 OP 代码的两个问题之一,请参阅我的答案。
  • 啊哈。没有意识到客户端和服务器 API 如此不同。
  • 顺便说一句,Paramiko 只是客户端库。 ChannelSSHClient 都是客户端类

标签: python python-3.x ssh paramiko


【解决方案1】:
  1. SSH 中没有session 通道(除非您的服务器实现了一些非标准通道)。有sftpshellexec频道。

    你想使用exec频道。

  2. 而且你不需要在 Paramiko 中显式打开exec 频道。只需使用SSHClient.exec_command method

    SSHClient.exec_command(与Channel.exec_command相反)返回三元组。

例如见Python Paramiko - Run command:

s = paramiko.SSHClient()
s.load_system_host_keys()
s.connect(hostname, port, username, password)
command = 'ls -lah'
(stdin, stdout, stderr) = s.exec_command(command)

【讨论】:

    猜你喜欢
    • 2016-02-08
    • 2020-05-06
    • 1970-01-01
    • 2013-12-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多