如何将远程服务器/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本地路径也在远程主机中?
答案 0 :(得分:2)
在SFTP服务器(特别是OpenSSH)的广泛实现中,不支持远程复制文件。因此,即使Paramiko确实支持它(它没有),它可能对你没有任何用处。
SFTP的copy-file
扩展名,但很少有服务器/客户端库支持它。
请参阅draft-ietf-secsh-filexfer-extensions-00。
备选方案:
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()