title: Paramiko/Python: Channel closed when invoking shell
body: <p>I need to execute a command in a remote server using paramiko <code>Channel</code>. </p>
  
  <p>Code:</p>
  
  <pre><code>def handler(title, instructions, fields):
      if len(fields) &gt; 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
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre><code>Traceback (most recent call last):
    File "sftp.py", line 120, in &lt;module&gt;
      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.
  </code></pre>
  
  <p>Paramiko log:</p>
  
  <pre><code>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 &lt;built-in function openssl_sha256&gt;
  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
  </code></pre>
  
  <p>I tried solution below but did not help:
  <a href="https://github.com/paramiko/paramiko/issues/513#issuecomment-450426574" rel="nofollow noreferrer">https://github.com/paramiko/paramiko/issues/513#issuecomment-450426574</a></p>
  
  <p>Any thoughts please?</p>