简体   繁体   English

Python 脚本将文件和目录从一台远程服务器复制到另一台远程服务器

[英]Python script to copy file and directory from one remote server to another remote server

I am running a python script - ssh.py on my local machine to transfer file and directory from one remote server (ip = 35.189.168.20) to another remote server (ip = 10.243.96.94)我正在本地计算机上运行 python 脚本 - ssh.py 以将文件和目录从一台远程服务器(ip = 35.189.168.20)传输到另一台远程服务器(ip = 10.243.96.94)

This is how my code looks:这就是我的代码的样子:

HOST = "35.189.168.207"
USER = "sovith"
PASS = "xxx"
destHost = "10.243.96.94"
destUser = "root"
destPass = "xxx"

#SSH Connection
client1=paramiko.SSHClient()
client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client1.connect(HOST,username=USER,password=PASS)

#SFTP inside SSH server connection
with pysftp.Connection(host=destHost, username=destUser, password=destPass) as sftp:
    #put build directory - sftp.put_r(source, destination)
    sftp.put_r('/home/sovith/build' , '/var/tmp') 
    sftp.close()
client1.close()

Let me just tell you that all directory paths and everything is correct.让我告诉你所有目录路径和一切都是正确的。 I just feel that there's some logically mistake inside the code.我只是觉得代码内部存在一些逻辑错误。 The output i got after execution is:我执行后得到的output是:

Traceback (most recent call last):
  File "ssh.py", line 108, in <module>
    func()
  File "ssh.py", line 99, in func
    sftp.put_r('/home/sovith/nfmbuild' , '/var/tmp')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pysftp/__init__.py", line 427, in put_r
    os.chdir(localpath)
FileNotFoundError: [Errno 2] No such file or directory: '/home/sovith/build'

Can you just correct the mistake from my code or suggest any better method to accomplish the task.您能否纠正我的代码中的错误或建议任何更好的方法来完成任务。 In simpler words, i want a script to copy files and directory between two remote server.简单来说,我想要一个脚本在两个远程服务器之间复制文件和目录。

That's not possible.那是不可能的。 Not the way you do it.不是你这样做的方式。 The fact that you open a connection to one remote server, does not make the following code magically work, as if it was executed on that server.您打开与一台远程服务器的连接这一事实不会使以下代码神奇地工作,就好像它是在该服务器上执行的一样。 It still runs on the local machine .它仍然在本地机器上运行。 So the code is trying to upload local files (which do not exist).所以代码正在尝试上传本地文件(不存在)。


There's actually no way to transfer files between two remote SFTP servers from local machine.实际上没有办法在本地机器的两个远程 SFTP 服务器之间传输文件。

In general, you will need to download the files from the first server to a local temporary directory.通常,您需要将文件从第一台服务器下载到本地临时目录。 And then upload them to the second server.然后将它们上传到第二个服务器。
See Python PySFTP transfer files from one remote server to another remote server请参阅Python PySFTP 将文件从一台远程服务器传输到另一台远程服务器


Another option is to connect to one remote server using SSH and then run SFTP client on the server to transfer the files to/from the second server.另一种选择是使用 SSH 连接到一个远程服务器,然后在服务器上运行 SFTP 客户端以将文件传输到第二个服务器或从第二个服务器传输文件。

But that's not copying from one SFTP server to another SFTP server.但这不是从一个 SFTP 服务器复制到另一个 SFTP 服务器。 That's copying from one SSH server to SFTP server.这是从一台 SSH 服务器复制到 SFTP 服务器。 You need an SSH access, mere SFTP access is not enough.您需要一个 SSH 访问权限,仅 SFTP 访问权限是不够的。

To execute a command of a remote SSH server, use pysftp Connection.execute .要执行远程 SSH 服务器的命令,请使用 pysftp Connection.execute Though using pysftp to execute a command on a server is a bit overkill.虽然使用 pysftp 在服务器上执行命令有点矫枉过正。 You can use directly Paramiko instead:您可以直接使用 Paramiko 代替:
Python Paramiko - Run command Python Paramiko - 运行命令
(pysftp is just a wrapper around Paramiko with more advanced SFTP functions) (pysftp 只是 Paramiko 的一个包装器,具有更高级的 SFTP 功能)

From your error message, can you confirm that the source directory /home/sovith/build (or /home/bgnanasekaran/nfmbuild ) exists?从您的错误消息中,您能否确认源目录/home/sovith/build (或/home/bgnanasekaran/nfmbuild )存在?

A good practice would be to check that before the call to sftp.put_r() , ie:一个好的做法是在调用sftp.put_r()之前检查它,即:

from pathlib import Path

d = Path('/home/sovith/build')
if d.exists():
  sftp.put_r('/home/sovith/build' , '/var/tmp')
else:
  print("Your source doesn't exist")

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

相关问题 使用fabric将文件从一个远程服务器复制到另一个远程服务器? - Using fabric to copy file from one remote server to another remote server? Bash脚本传输到Python-将日志从服务器复制到另一台远程服务器 - Bash script transfer to Python - copy logs from server to another remote server 我需要编写一个 python 脚本,它将建立从一台服务器(server1)到另一台远程服务器(server2)的无密码连接 - I need to write a python script which will make a passwordless connection from one server (server1) to another remote server(server2) Python PySFTP将文件从一台远程服务器传输到另一台远程服务器 - Python PySFTP transfer files from one remote server to another remote server Python脚本将文件上传到远程服务器 - Python script to upload a file to a remote server 用于在远程服务器中创建 zip 文件的 Python 脚本 - Python script for creating zip file in remote server 将InMemory文件复制到远程服务器 - Copy InMemory file to remote server 如何从Windows系统将文件从python脚本复制到任何其他远程服务器? - How do I copy files from windows system to any other remote server from python script? 如何使用 SCP 或 SSH 将文件复制到 Python 中的远程服务器? - How to copy a file to a remote server in Python using SCP or SSH? 在Python上重命名远程服务器上的文件 - Rename file on remote server on Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM