Recursive directory download with Paramiko?

后端 未结 6 1506
甜味超标
甜味超标 2020-11-30 10:23

I want to download a directory with unknown contents recursively via SSH and have been trying Paramiko. I have seen several examples how to upload directories but none that

6条回答
  •  一向
    一向 (楼主)
    2020-11-30 11:12

    An old question, but a solution I came up with that works quite well, it's a little bit sloppy (typecasting and slashes and all) - but it does work.

    Note this uses fabric.api.local to make the directories in the destination.

    def sftp_get_recursive(path, dest, sftp=sftp):
        item_list = sftp.listdir(path)
        dest = str(dest)
    
        if not os.path.isdir(dest):
            local("mkdir %s" % dest)
    
        for item in item_list:
            item = str(item)
    
            if is_directory(path + "/" + item, sftp):
                sftp_get_recursive(path + "/" + item, dest + "/" + item, sftp)
            else:
                sftp.get(path + "/" + item, dest + "/" + item)
    

提交回复
热议问题