Paramiko / Python:调用Shell时通道关闭

时间:2019-04-04 13:22:01

标签: python sftp paramiko

我需要使用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

Stacktrace:

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.

Paramiko日志:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

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

尽管可以自动执行命令,但实际上应该使用exec通道。
参见Python Paramiko - Run command

shell通道用于实现交互式会话(例如,如果您正在实现自己的SSH终端,那么实际上很少要做)。

答案 1 :(得分:-1)

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

所花费的时间和过多的错误只是为了猜测为什么通道在打开后立即关闭,或者验证失败使这种方式难以继续。