Paramiko exec_command 失败,'NoneType' object is not iterable

Paramiko exec_command fails with 'NoneType' object is not iterable

我是 Paramiko 的新手。我正在尝试创建一个简单的脚本,允许任何人使用他们的 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

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

  1. SSH 中没有 session 通道(除非您的服务器实现了一些非标准通道)。有 sftpshellexec 个频道。

    您想使用 exec 频道。

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

    SSHClient.exec_command(与Channel.exec_command相反)returns 3-touple.

参见示例 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)