简体   繁体   English

如何使用SCP或SSH将完整目录递归复制到Python(paramiko)中的远程服务器?

[英]How to copy a complete directory recursively to a remote server in Python (paramiko) using SCP or SSH?

I have a directory containing some files and sub-directories on my local machine that is generated by a daily Python script running. 我的本地计算机上有一个包含一些文件和子目录的目录,该目录由日常运行的Python脚本生成。 And then I want to copy all those generated files in a directory to the server and then run some series of commands on it by ssh using python package paramiko. 然后,我想将目录中所有这些生成的文件复制到服务器,然后使用python软件包paramiko通过ssh在该服务器上运行一系列命令。

I want to add little code to securely copy that entire directory and files and sub-directories inside it to to my server over SSH/SCP using the paramiko package of python. 我想添加一些代码,以使用python的paramiko软件包通过SSH / SCP将整个目录以及其中的文件和子目录安全地复制到我的服务器。 I can send the individual files using the same but unable to send the entire directory and its content using paramiko. 我可以使用相同的文件发送单个文件,但是无法使用paramiko发送整个目录及其内容。 It gives me IOError saying it's a directory. 它给我IOError说这是一个目录。

IOError: [Errno 21] Is a directory: 'temp'

Here is the my code: 这是我的代码:

from paramiko import SSHClient, AutoAddPolicy
from scp import SCPClient

class SSh(object):

    def __init__(self, address, username, password, port=22):
        print "Connecting to server."
        self._address = address
        self._username = username
        self._password = password
        self._port = port
        self.sshObj = None
        self.connect()
        self.scp = SCPClient(self.sshObj.get_transport())

    def sendCommand(self, command):
        if(self.sshObj):
            stdin, stdout, stderr = self.sshObj.exec_command(command)
            while not stdout.channel.exit_status_ready():
                # Print data when available
                if stdout.channel.recv_ready():
                    alldata = stdout.channel.recv(1024)
                    prevdata = b"1"
                    while prevdata:
                        prevdata = stdout.channel.recv(1024)
                        alldata += prevdata

                    print str(alldata)
        else:
            print "Connection not opened."

    def connect(self):
        try:
            ssh = SSHClient()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            ssh.connect(self._address, port=self._port, username=self._username, password=self._password, timeout=20, look_for_keys=False)
            print 'Connected to {} over SSh'.format(self._address)
            return True
        except Exception as e:
            ssh = None
            print "Unable to connect to {} over ssh: {}".format(self._address, e)
            return False
        finally:
            self.sshObj = ssh

if __name__ == "__main__":
    # Parse and check the arguments
    ssh = SSh('10.24.143.190', 'username', 'password')
    ssh.scp.put("Valigrind_BB.py") # This works perfectly fine
    ssh.scp.put("temp") # IOError over here Is a directory
    ssh.sendCommand('ls')

Thank you in advance. 先感谢您。

Looking at the source code for SCP, it appears you can pass the parameter "recursive" as a bool to the put() method to specify transferring the contents of a directory recursively. 查看SCP的源代码,您似乎可以将参数“ recursive”作为布尔值传递给put()方法,以指定递归传输目录的内容。 Here is a link to the source code I am talking about. 这里是我正在谈论的源代码的链接。 Try changing last part of your code to the following: 尝试将代码的最后一部分更改为以下内容:

if __name__ == "__main__":
    # Parse and check the arguments
    ssh = SSh('10.24.143.190', 'username', 'password')
    ssh.scp.put("Valigrind_BB.py") # This works perfectly fine
    ssh.scp.put("temp", recursive=True) # IOError over here Is a directory
    ssh.sendCommand('ls')

Also, if you just want to transfer files, you can try rsync as an alternative. 另外,如果您只想传输文件,则可以尝试使用rsync作为替代。 The modification to your code above should work though. 修改上面的代码应该可以。 I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 SCP 或 SSH 将文件复制到 Python 中的远程服务器? - How to copy a file to a remote server in Python using SCP or SSH? 使用 Python Paramiko SSH 服务器实现 SCP 服务器 - Implementing SCP server with Python Paramiko SSH server 如何使用Paramiko Python通过SSH从远程服务器复制文件? FileNotFoundError: [WinError 3] 系统找不到指定的路径 - How to copy file from remote server over SSH using Paramiko Python? FileNotFoundError: [WinError 3] The system cannot find the path specified SSH Paramiko命令未更改远程服务器中的目录 - SSH Paramiko command is not changing the directory in remote server 使用Python Paramiko在后台运行远程SSH服务器的过程 - Running process of remote SSH server in the background using Python Paramiko scp在python的paramiko中的嵌套ssh会话中 - scp in a nested ssh session in python's paramiko 使用 paramiko 将文件从远程目录复制到远程子目录 - Copy file from remote dir to remote sub directory using paramiko SCP与paramiko,使用不同的远程和本地目录 - SCP with paramiko, using different remote and local directories 使用 Python Paramiko 通过 ssh/sftp 将文件复制到具有不同文件名的服务器 - Copy a file to server with a different filename using Python Paramiko over ssh/sftp 如何使用 Python 和 Paramiko 创建 SSH 隧道? - How to create a SSH tunnel using Python and Paramiko?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM