title: How to keep ssh session not expired using paramiko?
body: <p>I intend to run several commands on remote host using paramiko, but the ssh session closed after running a command.<br>
  The code listed below:  </p>
  
  <pre><code>from paramiko import SSHClient  
  import paramiko  
  ssh = SSHClient()
  ssh.load_system_host_keys()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(host, 22, user, passwd, timeout=3)
  stdin, stdout, stderr = ssh.exec_command('uname -a')
  </code></pre>
  
  <p>So is there any way to stop the ssh session from closing? Or any alternatives for paramiko?  </p>
  
  <p><strong>Update</strong>:<br>
  I was able to keeping calling <code>exec_command</code> on my Macbook when connected to a Linux server, but the ssh session closed automatically after <code>exec_command</code> once on a Linux server when connected to a <strong>switch</strong> and raised an<br>
  <code>SSHException: paramiko.ssh_exception.SSHException: SSH session not active</code>  </p>
  
  <pre><code>&gt;&gt;&gt; print ssh.get_transport()  
  &gt;&gt;&gt; &lt;paramiko.Transport at 0xf00216d0L (unconnected)&gt;  
  &gt;&gt;&gt; print ssh.get_transport().is_active()  
  &gt;&gt;&gt; False  
  &gt;&gt;&gt; print ssh.get_transport().is_authenticated()  
  &gt;&gt;&gt; False
  </code></pre>
  
  <p>Is there any way to keep paramiko ssh session active all the time?  </p>
  
  <p>The paramiko debug mode info returned as following:  </p>
  
  <blockquote>
    <p>starting thread (client mode): 0x2657e10L<br>
    Connected (version 1.99, client Comware-5.20)<br>
    kex algos:[u'diffie-hellman-group-exchange-sha1', u'diffie-hellman- group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa'] client encrypt:[u'aes128-cbc', u'3des-cbc', u'des-cbc'] server encrypt:[u'aes128-cbc', u'3des-cbc', u'des-cbc'] client mac:[u'hmac-sha1', u'hmac-sha1-96', u'hmac-md5', u'hmac-md5-96'] server mac:[u'hmac-sha1', u'hmac-sha1-96', u'hmac-md5', u'hmac-md5-96'] client compress:[u'none'] server compress:[u'none'] client lang:[u''] server lang:[u''] kex follows?False<br>
    Ciphers agreed: local=aes128-cbc, remote=aes128-cbc<br>
    using kex diffie-hellman-group14-sha1; server key type ssh-rsa; cipher: local aes128-cbc, remote aes128-cbc; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none<br>
    Switch to new keys ...<br>
    userauth is OK<br>
    Authentication (password) successful!<br>
    [chan 0] Max packet in: 32768 bytes<br>
    [chan 1] Max packet in: 32768 bytes<br>
    [chan 0] Max packet out: 32496 bytes<br>
    Secsh channel 0 opened.<br>
    Secsh channel 2 open FAILED:<br>
    Resource shortage: Resource shortage<br>
    [chan 0] Sesch channel 0 request ok<br>
    [chan 0] EOF sent (0)</p>
  </blockquote>