title: Python - How to run a for loop over files in a directory accessed via ssh?
body: <p>I'm trying to write a function where it would ssh into a remote host and for every file in a directory, process it.</p>
  
  <p>For example, my current function for the for loop is:</p>
  
  <pre><code>import fnmatch, os    
  def process_logfiles_in_remote_directory(remotedir):
      for filename in os.listdir(remotedir):
          if fnmatch.fnmatch(filename, '*.log'):
              filename = os.path.join(remotedir, filename)
              ## Do something here...
  </code></pre>
  
  <p>How do I wrap the above function in a ssh connection, something in the line of this:</p>
  
  <pre><code>ssh_client = paramiko.SSHClient()
  ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh_client.connect("www.remotehost.com", username="admin", password="testing")
  stdin, stdout, stderr = ssh_client.exec_command(process_logfiles_in_remote_directory('/tmp'))
  ssh_client.close()
  </code></pre>