gpt4 book ai didi

python - Paramiko exec_command 失败, 'NoneType' 对象不可迭代

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:50 24 4
gpt4 key购买 nike

我是 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 channel (除非您的服务器实现了一些非标准 channel )。有sftpshellexec channel 。

    您想使用exec channel 。

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

    SSHClient.exec_command(与 Channel.exec_command 相反)返回 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)

关于python - Paramiko exec_command 失败, 'NoneType' 对象不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51103887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com