0

I'm trying to write a function where it would ssh into a remote host and for every file in a directory, process it.

For example, my current function for the for loop is:

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...

How do I wrap the above function in a ssh connection, something in the line of this:

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()
kawadhiya21
  • 2,458
  • 21
  • 34
user2766739
  • 467
  • 2
  • 8
  • 15
  • Possible duplicate of [How to list all the folders and files in the directory after connecting through sftp in python](http://stackoverflow.com/questions/12295551/how-to-list-all-the-folders-and-files-in-the-directory-after-connecting-through) – kawadhiya21 Feb 14 '17 at 06:40

1 Answers1

0

It would probably be easier to copy your Python script (the one with the loop) onto your remote machine using SCP and then running the copied Python script on the remote machine.

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...
if __name__ == '__main__':
  process_logfiles_in_remote_directory('/tmp')

Then on your local, you may do:

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("python <filename>.py")
ssh_client.close()

This has a hardcoded path to /tmp though. You could make your file accept command line arguments in order to handle any path you pass at run time.

Anoop R Desai
  • 712
  • 5
  • 18