【问题标题】:Python Paramiko SSH run commandPython Paramiko SSH 运行命令
【发布时间】:2022-03-11 11:04:50
【问题描述】:

我想运行简单的命令来测试 ssh 连接。有关于标准输入/输出/错误的错误。代码和错误信息如下。

有人可以帮我吗?

代码

import paramiko
import sys

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('x.x.x.x', port=2222, username='user', password='pass')
stdin, stdout, stderr = client.exec_command('exit')

print stdout
client.close()

错误信息

Traceback (most recent call last):
File "C:/Users/EXT03D3912/PycharmProjects/paramiko_ssh1/paramiko_ssh1.py",           line 9, in <module>
stdin, stdout, stderr = client.exec_command('exit')
File "build\bdist.win32\egg\paramiko\client.py", line 343, in exec_command
File "build\bdist.win32\egg\paramiko\channel.py", line 212, in exec_command
File "build\bdist.win32\egg\paramiko\channel.py", line 1081, in     _wait_for_event
EOFError

谢谢, 哈克丹

【问题讨论】:

    标签: python ssh stdin paramiko execcommand


    【解决方案1】:

    由于您已从终端注销,它会给出 EOFError AFAIK,您只需关闭连接,它就会自动关闭您的会话。

    考虑下面的代码

    import paramiko
    import sys
    
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('x.x.x.x', port=2222, username='user', password='pass')
    stdin, stdout, stderr = client.exec_command('ls')
    
    print stdout
    client.close()
    

    【讨论】:

    • 我改了里面的exec_command,还是一样的错误。
    • IDE(PyCharm) 将“stdin、stdout、stderr”指出为错字。这些定义有什么问题吗?