Search code examples
pythonpython-2.7sshscpparamiko

How to copy a folder using paramiko from remote to local?


Here i have mentioned a snippet which puts a file and same logic can be used to get files from the remote.

from paramiko import SSHClient
from paramiko import AutoAddPolicy
from scp import SCPClient
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect('192.168.150.4',username='researcher',password='help')

#SCP connectin
scp = SCPClient(ssh.get_transport())
scp.put('test.txt')

Now my remote has a folder inside 2 sub-directories,which i want to copy to my local machine,But i dont know where exactly scp is contacting.Is there a way to execute pwd and migrate in remote using paramiko.Also i have used this Wrapper to get the job done but it to does not did what it was asked to do,it is simply putting the file but never returning the file.

Is there any better way to get either folder or reccursively download the files and copy to my local machine.?


Solution

  • You should create a paramiko.SFTPClient and then use listdir() or listdir_iter() to enumerate the files in the directory you want to copy. Then copy them one by one. There's no recursive copy operation built in.