使用Python中的Paramiko进行递归目录复制

时间:2014-02-04 10:40:28

标签: python sftp paramiko

我是Python脚本的新手。我需要从本地机器(Windows)复制几个文件夹到Linux服务器。截至目前,我正在通过打开WinSCP控制台来复制文件夹。我需要自动化这个过程。我使用Paramiko模块库在Python中编写了以下代码。

import paramiko
import os
transport = paramiko.Transport(('10.10.10.10', 22))
transport.connect(username='weblogic', password='weblogic')
sftp = paramiko.SFTPClient.from_transport(transport)
filepath = '/apps/logs'
localpath = 'C:\\Users\\Public\\test'
sftp.put(localpath,filepath)

以上操作不正常并给出以下错误。你能帮我复制一下windows路径C:\Users\Public\test中的文件夹到Linux服务器路径/apps/logs吗?

Traceback (most recent call last):
  File "C:\Users\Desktop\python\execute_script.py", line 28, in <module>
    sftp.put(localpath,filepath)
  File "C:\Python27\lib\paramiko\sftp_client.py", line 548, in put
    fl = file(localpath, 'rb')
IOError: [Errno 13] Permission denied: 'C:\\Users\\Public\\test'

5 个答案:

答案 0 :(得分:6)

请从链接https://gist.github.com/johnfink8/2190472查看以下代码。我在代码段中使用了put_all方法。

import paramiko
import socket
import os
from stat import S_ISDIR

class SSHSession(object):
    # Usage:
    # Detects DSA or RSA from key_file, either as a string filename or a
    # file object.  Password auth is possible, but I will judge you for 
    # using it. So:
    # ssh=SSHSession('targetserver.com','root',key_file=open('mykey.pem','r'))
    # ssh=SSHSession('targetserver.com','root',key_file='/home/me/mykey.pem')
    # ssh=SSHSession('targetserver.com','root','mypassword')
    # ssh.put('filename','/remote/file/destination/path')
    # ssh.put_all('/path/to/local/source/dir','/path/to/remote/destination')
    # ssh.get_all('/path/to/remote/source/dir','/path/to/local/destination')
    # ssh.command('echo "Command to execute"')

    def __init__(self,hostname,username='root',key_file=None,password=None):
        #
        #  Accepts a file-like object (anything with a readlines() function)  
        #  in either dss_key or rsa_key with a private key.  Since I don't 
        #  ever intend to leave a server open to a password auth.
        #
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((hostname,22))
        self.t = paramiko.Transport(self.sock)
        self.t.start_client()
        keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
        key = self.t.get_remote_server_key()
        # supposed to check for key in keys, but I don't much care right now to find the right notation
        if key_file is not None:
            if isinstance(key,str):
                key_file=open(key,'r')
            key_head=key_file.readline()
            key_file.seek(0)
            if 'DSA' in key_head:
                keytype=paramiko.DSSKey
            elif 'RSA' in key_head:
                keytype=paramiko.RSAKey
            else:
                raise Exception("Can't identify key type")
            pkey=keytype.from_private_key(key_file)
            self.t.auth_publickey(username, pkey)
        else:
            if password is not None:
                self.t.auth_password(username,password,fallback=False)
            else: raise Exception('Must supply either key_file or password')
        self.sftp=paramiko.SFTPClient.from_transport(self.t)

    def command(self,cmd):
        #  Breaks the command by lines, sends and receives 
        #  each line and its output separately
        #
        #  Returns the server response text as a string

        chan = self.t.open_session()
        chan.get_pty()
        chan.invoke_shell()
        chan.settimeout(20.0)
        ret=''
        try:
            ret+=chan.recv(1024)
        except:
            chan.send('\n')
            ret+=chan.recv(1024)
        for line in cmd.split('\n'):
            chan.send(line.strip() + '\n')
            ret+=chan.recv(1024)
        return ret

    def put(self,localfile,remotefile):
        #  Copy localfile to remotefile, overwriting or creating as needed.
        self.sftp.put(localfile,remotefile)

    def put_all(self,localpath,remotepath):
        #  recursively upload a full directory
        os.chdir(os.path.split(localpath)[0])
        parent=os.path.split(localpath)[1]
        for walker in os.walk(parent):
            try:
                self.sftp.mkdir(os.path.join(remotepath,walker[0]))
            except:
                pass
            for file in walker[2]:
                self.put(os.path.join(walker[0],file),os.path.join(remotepath,walker[0],file))

    def get(self,remotefile,localfile):
        #  Copy remotefile to localfile, overwriting or creating as needed.
        self.sftp.get(remotefile,localfile)

    def sftp_walk(self,remotepath):
        # Kindof a stripped down  version of os.walk, implemented for 
        # sftp.  Tried running it flat without the yields, but it really
        # chokes on big directories.
        path=remotepath
        files=[]
        folders=[]
        for f in self.sftp.listdir_attr(remotepath):
            if S_ISDIR(f.st_mode):
                folders.append(f.filename)
            else:
                files.append(f.filename)
        print (path,folders,files)
        yield path,folders,files
        for folder in folders:
            new_path=os.path.join(remotepath,folder)
            for x in self.sftp_walk(new_path):
                yield x

    def get_all(self,remotepath,localpath):
        #  recursively download a full directory
        #  Harder than it sounded at first, since paramiko won't walk
        #
        # For the record, something like this would gennerally be faster:
        # ssh user@host 'tar -cz /source/folder' | tar -xz

        self.sftp.chdir(os.path.split(remotepath)[0])
        parent=os.path.split(remotepath)[1]
        try:
            os.mkdir(localpath)
        except:
            pass
        for walker in self.sftp_walk(parent):
            try:
                os.mkdir(os.path.join(localpath,walker[0]))
            except:
                pass
            for file in walker[2]:
                self.get(os.path.join(walker[0],file),os.path.join(localpath,walker[0],file))
    def write_command(self,text,remotefile):
        #  Writes text to remotefile, and makes remotefile executable.
        #  This is perhaps a bit niche, but I was thinking I needed it.
        #  For the record, I was incorrect.
        self.sftp.open(remotefile,'w').write(text)
        self.sftp.chmod(remotefile,755)

答案 1 :(得分:0)

除了@ user1041177的答案之外,这里有一种方法可以在windows上使用linux主机(实际上并不确定哪种主机)。

我不知道为什么,但如果我将反斜杠保留在远程路径上,我会得到一个FileNotFoundException。唯一的工作方式是更换所有&#39; \&#39;通过&#39; /&#39;

也许有人可以告诉我避免这种情况的正确方法?

如果您遇到同样的问题,请在上面提供相同代码的一部分,以便为您提供面包屑:

UPDATE tbl_1 SET tlb_1.columX= tbl_1.columX + tbl_2.columY
顺便说一下,我没有在对象中使用这些功能,这就是为什么他们的对象是&#39; socket&#39;而不是自我&#39;因为我通过将SFTP套接字传递给它们来调用这些函数。

最后不得不说谢谢@ user1041177,像魅力一样工作。

答案 2 :(得分:0)

我试图从一个Windows框复制到一个linux框,并得到与上面@Apex相同的错误。我使用的是put_all方法,我不得不在部分代码上做一些“替换”。

def put_all(self,localpath,remotepath):
        remotepath = remotepath.replace('\\', '/')
        #  recursively upload a full directory
        os.chdir(os.path.split(localpath)[0])
        parent=os.path.split(localpath)[1]
        for walker in os.walk(parent):
            try:
                self.sftp.mkdir(os.path.join(remotepath,walker[0]).replace('\\', '/'))
            except:
                pass
            for file in walker[2]:
                self.put(os.path.join(walker[0],file).replace('\\', '/'),os.path.join(remotepath,walker[0],file).replace('\\', '/'))

答案 3 :(得分:0)

Paramiko不支持递归操作。

您可以使用pysftp。它是Paramiko的包装器,具有更多的Python-ish外观和感觉,并支持递归操作。参见

或者您可以将代码基于pysftp source code。或者查看我对Python pysftp get_r from Linux works fine on Linux but not on Windows的回答。

答案 4 :(得分:0)

我发现上述方法有一些缺点-首先,推杆/吸气器无法按您期望的方式运行-如果您想将 / foo / bar 放入 / some / folder ,因为它不允许您将文件从源文件夹放置到其他目标文件夹,所以您不能这样做-唯一可以做的就是放置 / foo / bar 进入 / some / bar 。另外,您必须将源指定为 / foo / bar ,将目的地指定为 / some ,以 / some / bar 结尾-我发现这很混乱,因为它不是大多数操作系统/ ftp系统如何处理放置/获取/复制/等的方式。因此,我改进了上面列出的答案:

如果您要从Windows转到Linux:

def put_dir(source, dest):
    source = os.path.expandvars(source).rstrip('\\').rstrip('/')
    dest = os.path.expandvars(dest).rstrip('\\').rstrip('/')

    for root, dirs, files in os.walk(source):
        for dir in dirs:
            try:
                sftp.mkdir(posixpath.join(dest, ''.join(root.rsplit(source))[1:].replace('\\', '/'), dir))
            except:
                pass
        for file in files:
            sftp.put(os.path.join(root, file), posixpath.join(dest, ''.join(root.rsplit(source))[1:].replace('\\', '/'), file))


source = '%USERPROFILE%\\Downloads\\'
dest = '/foo/bar'

put_dir(source, dest)

如果您只使用Windows,则将 posixpath.join 换成 os.path.join ,然后删除 .replace('\\',' /')

def put_dir(source, dest):
    source = os.path.expandvars(source).rstrip('\\').rstrip('/')
    dest = os.path.expandvars(dest).rstrip('\\').rstrip('/')

    for root, dirs, files in os.walk(source):
        for dir in dirs:
            try:
                sftp.mkdir(os.path.join(dest, ''.join(root.rsplit(source))[1:], dir))
            except:
                pass
        for file in files:
            sftp.put(os.path.join(root, file), os.path.join(dest, ''.join(root.rsplit(source))[1:], file))


source = '%USERPROFILE%\\Downloads\\'
dest = 'foo\\bar'

put_dir(source, dest)

使用try语句的原因是,如果文件夹已经存在,则sftp.mkdir会出错。