'Copy file from remote dir to remote sub directory using paramiko
How to copy a file in a remote server /maindir/fil1.txt
to a sub directory /maindir/subdir/file1.txt
i have the sftp
created using paramiko
. But it always check for the local path to copy from .
filename_full_path='/maindir/fil1.txt'
destfilename_full_path='/maindir/subdir/file1.txt'
sftp.put(filename_full_path, destfilename_full_path)
How to tell sftp that the local path is also in remote host?
Solution 1:[1]
You can try in the below way
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")
Solution 2:[2]
A core SFTP protocol does not support copying remote files.
There's draft of copy-data
/copy-file
extension to the SFTP protocol.
But in the most widespread OpenSSH SFTP server the copy-data
is supported by very recent version 9.0 only. Another servers that do support the extensions are ProFTPD mod_sftp
and Bitvise SFTP server.
So even if Paramiko did support it (it does not), it would probably not be of any use to you.
Alternatives:
- Download the folder and reupload it to a new location (a pure SFTP solution)
- Use
cp
command in a "exec" channel (not SFTP anymore, requires a shell access) – useSSHClient.exec_command
. - Many mistake copy and move. Moving a file to another folder is supported.
Solution 3:[3]
You can't copy or move files as we normally do in an operating system. You need to follow this to make a file transfer.
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sandeep Lade |
Solution 2 | |
Solution 3 | AgentLog |