使用paramiko将文件从远程目录复制到远程子目录

时间:2017-10-25 11:32:49

标签: python sftp paramiko

如何将远程服务器/maindir/fil1.txt中的文件复制到子目录/maindir/subdir/file1.txt我使用sftp创建了paramiko。但它总是检查要从中复制的本地路径。

filename_full_path='/maindir/fil1.txt'
destfilename_full_path='/maindir/subdir/file1.txt'
sftp.put(filename_full_path, destfilename_full_path)

如何告诉sftp本地路径也在远程主机中?

3 个答案:

答案 0 :(得分:2)

在SFTP服务器(特别是OpenSSH)的广泛实现中,不支持远程复制文件。因此,即使Paramiko确实支持它(它没有),它可能对你没有任何用处。

SFTP的copy-file扩展名,但很少有服务器/客户端库支持它。
请参阅draft-ietf-secsh-filexfer-extensions-00

备选方案:

  • 下载文件夹并将其重新上传到新位置(纯SFTP解决方案)
  • 在" exec"中使用cp命令通道(不再是SFTP,需要shell访问) - 使用SSHClient.exec_command

答案 1 :(得分:0)

您可以尝试以下方式

   a=paramiko.SSHClient()
   a.set_missing_host_key_policy(paramiko.AutoAddPolicy())
   a.connect('ip',username='user',password='passw')
   stdin, stdout, stderr = a.exec_command("cp /sourc/file /target/file")

答案 2 :(得分:0)

您不能像在操作系统中通常那样复制或移动文件。 您需要遵循此步骤进行文件传输。

import paramiko
ssh_client =paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=’hostname’,username=’something’,password=’something_unique’)

ftp_client=ssh_client.open_sftp()
ftp_client.put(‘localfilepath’,remotefilepath’)   #for Uploading file from local to remote machine
#ftp_client.get(‘remotefileth’,’localfilepath’)   for Downloading a file from remote machine

ftp_client.close()