title: SSH connection not persistent
body: <p>I have the following script which SSH's into a network server and executes some commands, for some reason the SSH connection opens but by the time the commands are executed it closes (I think), as a result the commands are failing with below error? Can anyone provide info how to make the SSH connection persistent?</p>
  
  <pre><code>#!/usr/bin/python
  import os
  import sys
  import json
  import fileinput
  import pwd
  from subprocess import Popen, PIPE, STDOUT
  import re
  import paramiko
  import MySQLdb
  
  resource = r'qca-cdit-01'
  ssh = paramiko.SSHClient()
  ssh.load_system_host_keys()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(resource, username='username', password='passwordname')
  #chan = ssh.get_transport().open_session()
  chan = ssh.invoke_shell()
  chan.get_pty()
  
  commandstringlist = \
      ['/local/mnt/workspace/LA_host_builds/AU_LINUX_ANDROID_LA.BF64.1.2.1_RB2.05.01.01.081.031_msm8992',
       'cd frameworks/base',
       'git fetch ssh://cdit@review-android.company.com:29418/platform/frameworks/base refs/changes/21/1260821/2 &amp;&amp; git cherry-pick FETCH_HEAD']
  for cmd_val in commandstringlist:
      #chan.exec_command(cmd_val)
      chan.send(cmd_val)
      print(chan.recv(1024))
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "ssh_test.py", line 21, in &lt;module&gt;
      chan.get_pty()
    File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 60, in _check
      return func(self, *args, **kwds)
    File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 177, in get_pty
      self._wait_for_event()
    File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 1086, in _wait_for_event
      raise e
  paramiko.ssh_exception.SSHException: Channel closed
  </code></pre>