【问题标题】:Paramiko/Python: Channel closed when invoking shellParamiko / Python:调用shell时通道关闭
【发布时间】:2019-08-26 06:38:05
【问题描述】:

我需要使用 paramiko Channel 在远程服务器中执行命令。

代码:

def handler(title, instructions, fields):
    if len(fields) > 1:
        raise sftp.SSHException("Expecting one field only.")
    return [password]


def create_sftp_client():
    #from transport object

    sftp.util.log_to_file("paramiko", level="DEBUG")

    transport = sftp.Transport(('myhost', 2222),
    default_max_packet_size=10000, default_window_size=10000)
    transport.connect(username='myuser', password='mypassword')
    transport.auth_interactive(username, handler)
    channel = transport.open_channel("session")
    channel.invoke_shell()
    channel.send('ls\n')

    return channel

堆栈跟踪:

Traceback (most recent call last):
  File "sftp.py", line 120, in <module>
    sftp_client = create_sftp_client()
  File "sftp.py", line 75, in create_sftp_client
    channel.invoke_shell()
  File "...\Python\Python37\Lib\site-packages\paramiko\channel.py", line 72, in _check
    return func(self, *args, **kwds)
  File "...Python\Python37\Lib\site-packages\paramiko\channel.py", line 230, in invoke_shell
    self._wait_for_event()
  File "...Python\Python37\Lib\site-packages\paramiko\channel.py", line 1208, in _wait_for_event
    raise e
paramiko.ssh_exception.SSHException: Channel closed.

帕拉米科日志:

DEB [20190404-09:16:14.653] thr=1   paramiko.transport: starting thread (client mode): 0x433b44e0
DEB [20190404-09:16:14.653] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.4.2
DEB [20190404-09:16:14.708] thr=1   paramiko.transport: Remote version/idstring: SSH-2.0-Server
INF [20190404-09:16:14.708] thr=1   paramiko.transport: Connected (version 2.0, client Server)
DEB [20190404-09:16:14.709] thr=1   paramiko.transport: kex algos:['ecdh-sha2-nistp521', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp256', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group18-sha512', 'diffie-hellman-group17-sha512', 'diffie-hellman-group16-sha512', 'diffie-hellman-group15-sha512', 'diffie-hellman-group14-sha256', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'aes192-cbc', 'aes256-cbc'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'aes192-cbc', 'aes256-cbc'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib', 'zlib@openssh.com'] server compress:['none', 'zlib', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEB [20190404-09:16:14.709] thr=1   paramiko.transport: Kex agreed: ecdh-sha2-nistp256
DEB [20190404-09:16:14.709] thr=1   paramiko.transport: HostKey agreed: ssh-rsa
DEB [20190404-09:16:14.709] thr=1   paramiko.transport: Cipher agreed: aes128-ctr
DEB [20190404-09:16:14.709] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20190404-09:16:14.709] thr=1   paramiko.transport: Compression agreed: none
DEB [20190404-09:16:15.004] thr=1   paramiko.transport: kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
DEB [20190404-09:16:15.004] thr=1   paramiko.transport: Switch to new keys ...
DEB [20190404-09:16:15.004] thr=2   paramiko.transport: Attempting password auth...
DEB [20190404-09:16:15.155] thr=1   paramiko.transport: userauth is OK
INF [20190404-09:16:15.728] thr=1   paramiko.transport: Authentication continues...
DEB [20190404-09:16:15.728] thr=1   paramiko.transport: Methods: ['keyboard-interactive']
DEB [20190404-09:16:15.786] thr=1   paramiko.transport: userauth is OK
INF [20190404-09:16:16.213] thr=1   paramiko.transport: Authentication (keyboard-interactive) successful!
DEB [20190404-09:16:16.214] thr=2   paramiko.transport: [chan 0] Max packet in: 10000 bytes
DEB [20190404-09:16:16.268] thr=1   paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20190404-09:16:16.268] thr=1   paramiko.transport: Secsh channel 0 opened.
DEB [20190404-09:16:16.323] thr=1   paramiko.transport: [chan 0] EOF sent (0)
DEB [20190404-09:16:16.350] thr=1   paramiko.transport: EOF in transport thread

我尝试了以下解决方案,但没有帮助: https://github.com/paramiko/paramiko/issues/513#issuecomment-450426574

有什么想法吗?

【问题讨论】:

  • 你为什么要标记问题sftp
  • 您想发送文件,还是只执行一个命令?如果是后者,您应该使用 SSHClient 对象。至少在这种情况下您不需要调用 shell - 您可以在连接后直接执行命令。
  • 理想情况下我需要下载、上传和执行命令(目录列表等)
  • 要上传/上传,请使用 SFTP。您不能使用“shell”或任何命令来传输文件。 ---要执行(shell)命令,请参阅我的答案。 --- 你的问题太笼统了。

标签: python sftp paramiko


【解决方案1】:

没有session SSH 通道。有shell频道。

虽然为了自动执行命令,您实际上应该使用exec 频道。
Python Paramiko - Run command

shell 频道旨在实现交互式会话(例如,如果您正在实现自己的 SSH 终端,您实际上很少想要这样做)。

【讨论】:

  • 除非我读错了文档,否则channel 可以使用kind="session"transport 打开:docs.paramiko.org/en/2.4/api/…
  • 对不起,你是对的!我误解了参数的目的。 Transport.open_channel 不是您真正想直接调用的方法。您应该致电Transport.open_sessionTransport.open_sftp_client。 --- 无论如何,你的代码对我来说很好用。 --- 您可以使用常规 SSH 客户端打开与该服务器的 shell 会话吗? --- 打开 SFTP 通道有效吗? sftp = SFTPClient.from_transport(transport) --- 你的函数被称为 create_sftp_client 有点令人困惑,但你正在打开一个 shell 会话。
  • 你真正想做什么? ls 是什么?那是shell命令还是“SFTP命令”?如果是后者,请阅读my answerExecuting SFTP commands using Paramiko in Python
  • 最终目标是能够连接一个get/put文件。现在我改用SSHClient():pastebin.com/TE3CUthc Stacktrace:pastebin.com/MNvMu57QParamiko log:pastebin.com/79tRZjiA我认为服务器不接受密码验证,我的猜测是:# /etc/ssh/sshd_config PasswordAuthentication no ChallengeResponseAuthentication yesIs it possible to handle interactive auth when the sftp 客户端不是从transport 创建的?
  • 我很可能在这种情况下亚马逊默认禁用 ssh 密码身份验证:serverpilot.io/docs/how-to-enable-ssh-password-authentication 虽然我无权确认。必须等待我的 devops 这样做....
【解决方案2】:

我决定在那个特定情况下不使用 paramiko。我将使用另一个库(可能是 ssh2)。

仅仅为了猜测通道在打开或身份验证失败后立即关闭的原因所投入的时间和过多的错误使得很难继续这种方式。

【讨论】:

  • 1) 这不能回答问题。 2) 无论您将使用哪个库,您都必须了解shellexecsftp 通道的用途以及何时使用它们。这就是 SSH 的工作原理。这不是 Paramiko 特有的。如果你切换到另一个库,你只需要从头开始,处理和现在一样的东西。 Paramiko 是迄今为止使用最广泛的 Python SSH 库,您会发现使用它的示例比其他库要多。
  • 明白了。正如我之前解释的,理想情况下我必须执行和下载/上传数据,这就是我测试这两个用例的原因。无论我使用client 还是transport,我都会碰壁。我不习惯网络编程,所以我想这是另一块慢下来的石头。答案似乎并不关心我发布的日志/信息(这里的真正问题是为什么频道被关闭?)。确实,教育新手很重要。但是,试图回答真正的问题是最紧迫的优先事项。老年人往往会忘记这一点。话虽如此,感谢所有提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多