0

A Directory containing set of files, sub-dir and sub-dir containing files, I need to check whether these files are actually generated on remote location.

say [a.log,b.log,c.log,tmp_folder] and tmp_folder containing d.log,e.log files

Currently, I'm logging into remote machine and getting list of files but I'm unable to match these and also I'm unable to sftp that entire directory to local machine.

remote_dir = "some/location/path/time_stamp_10101010"
local_dir = "location/on/local/path"

Need to copy that entire directory time_stamp_10101010 in local_path.

import re,os,sys
import paramiko


remote_dir = "some/location/path/time_stamp_10101010"
local_dir = "location/on/local/path" 

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host_ip, username=username,key_filename=key_filename,port=22)
stdin, stdout, stderr = ssh.exec_command("ls")
sftp_client = ssh.open_sftp()
dir_items = sftp_client.listdir_attr(remote_dir)
for item in dir_items:
    print item
    if os.path.isfile(os.path.join(remote_dir, item)):
            remote_path = remote_dir + '/' + item.filename
            local_path = os.path.join(local_dir, item.filename)
            sftp_client.put_dir(remote_path, local_path)
    # If item is a directory here and if it contains sub-directory and files
    # How can I implement to copy recursively till all sub-directories and files are included.

Am I doing it right ? or Is there any better apparoach ? How do i validate the files, sub-dirs generated in a directory matches the standard set (as mentioned above) ? reg-ex would be better i feel.

StackGuru
  • 471
  • 1
  • 9
  • 25

1 Answers1

0

You can use recursive function like this:

def recursive_walk(path, remote_directory="", local_directory=""):
    if os.path.isfile(path):
        print("I am a file take me anywhere", path)

        return
    for item in os.listdir(path):
        item_path = os.path.join(path, item)
        recursive_walk(item_path)


recursive_walk('.')