Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory transfers with Paramiko

How do you use Paramiko to transfer complete directories? I'm trying to use:

sftp.put("/Folder1","/Folder2") 

which is giving me this error -

Error : [Errno 21] Is a directory

like image 629
fixxxer Avatar asked Dec 10 '10 14:12

fixxxer


People also ask

What can you do with paramiko?

Paramiko helps you automate repetitive system administration tasks on remote servers. More advanced Paramiko programs send the lines of a script one at a time. It does this rather than transacting all of a command, such as df or last , synchronously to completion.

How do I change directory in paramiko?

readlines() will return the directory that you changed to. Show activity on this post. Well paramiko creates an instance of shell and all the commands that you wish to execute in paramiko have to be given in that instance of shell only. For example: Let us say I have some folder in the directory I am in.

What is paramiko transport?

class paramiko.Transport. An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called Channels, across the session. class paramiko.SSHClient. A high-level representation of a session with an SSH server.

What is SFTP in paramiko?

SFTP client object. Used to open an SFTP session across an open SSH Transport and perform remote file operations. Instances of this class may be used as context managers.


1 Answers

You can subclass paramiko.SFTPClient and add the following method to it:

import paramiko import os  class MySFTPClient(paramiko.SFTPClient):     def put_dir(self, source, target):         ''' Uploads the contents of the source directory to the target path. The             target directory needs to exists. All subdirectories in source are              created under target.         '''         for item in os.listdir(source):             if os.path.isfile(os.path.join(source, item)):                 self.put(os.path.join(source, item), '%s/%s' % (target, item))             else:                 self.mkdir('%s/%s' % (target, item), ignore_existing=True)                 self.put_dir(os.path.join(source, item), '%s/%s' % (target, item))      def mkdir(self, path, mode=511, ignore_existing=False):         ''' Augments mkdir by adding an option to not fail if the folder exists  '''         try:             super(MySFTPClient, self).mkdir(path, mode)         except IOError:             if ignore_existing:                 pass             else:                 raise 

To use it:

transport = paramiko.Transport((HOST, PORT)) transport.connect(username=USERNAME, password=PASSWORD) sftp = MySFTPClient.from_transport(transport) sftp.mkdir(target_path, ignore_existing=True) sftp.put_dir(source_path, target_path) sftp.close() 
like image 153
skoll Avatar answered Sep 22 '22 18:09

skoll