Copy file from remote dir to remote sub directory using Paramiko - python

How to copy a file in a remote server /maindir/fil1.txt to a sub directory /maindir/subdir/file1.txt. I have the SFTP implemented 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?

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) – use SSHClient.exec_command.
Many mistake copy and move. Moving a file to another folder is supported.

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")

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()

Related

Transfer files in remote folder to remote folder in SFTP server using python ( pysftp) [duplicate]

How to copy a file in a remote server /maindir/fil1.txt to a sub directory /maindir/subdir/file1.txt. I have the SFTP implemented 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?
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) – use SSHClient.exec_command.
Many mistake copy and move. Moving a file to another folder is supported.
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")
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()

How to compute MD5 hash for a file on the remote server using paramiko

I have achieved how to download a file using SFTP and generate an MD5 hash of the downloaded file locally.
I am trying to upload a file to an SFTP Server and generate its MD5 hash when it's on the server and then download the file and its MD5 hash from the remote server.
How can I computer the MD5 hash on the remote SFTP server?
Very few FTP/SFTP servers support calculation of remote file checksum.
You can try first (my) WinSCP GUI SFTP/FTP client to see if your FTP/SFTP server does. WinSCP supports many standard and non-standard APIs for checksum calculation. So if your server does support such API, WinSCP should be able to make use of it.
https://winscp.net/eng/docs/ui_properties#checksum
You wrote about FTP. I've covered that already in:
MD5 value for file in a remote FTP server using Python ftplib
Though you possibly mistake SFTP and FTP. As Paramiko is an SSH/SFTP client. FTP and SFTP and two completely different protocols.
For SFTP/Paramiko, see:
How to check if Paramiko successfully uploaded a file to an SFTP server?
What SFTP server implementations support check-file extension
If your FTP/SFTP server does not have remote file check sum calculation API, you cannot use FTP/SFTP for this. But commonly, if you have SFTP access (but not so with FTP), you also might have shell access. So you can use shell commands for the checksum calculation.
See Comparing MD5 of downloaded files against files on an SFTP server in Python

How do i copy a python file from my windows to Linux subsystem after connect ssh to my server?

At first, I can use this to access my files.
cd /mnt/c/Users/
but after I connected ssh to my server. I'm no longer get to access files in my windows.
How do I can run my python file after being connected to a server from my windows?
Assuming that your SSH server is located outside of your computer, you need to copy the file to the server. You can do it with scp How to use SCP

How to create a zip file on SFTP server from files on the server using pysftp

I want to write a Python script that connects to the remote SFTP server and creates a ZIP file in the remote server which consists of specific files present in remote server itself. I have written the below script, I m using pysftp for this,
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
print("Connection succesfully stablished ... ")
files = ['user/1.csv','user/2.csv','user/test.csv', 'user/test1.csv']
with zipfile.ZipFile('zipfile.zip', 'w') as zipMe:
for file in files:
zipMe.write(file, file.split('/')[-1], compress_type=zipfile.ZIP_DEFLATED)
But instead of creating a ZIP file in the remote server, an ZIP file gets created in the local. Can anyone please help me with this?
Indeed, you code compresses local files to local ZIP archive. Note how your code never uses the sftp variable.
If you want to compress remote files to remote ZIP archive using a local Python script, you have to download the remote files, zip them locally and upload the ZIP archive.
You can do that all that "in-memory" without actually storing the remote files or the ZIP file physically to the local system. But you still need to do all the transfers. So it will be terribly inefficient.
You better execute zip command on the remote server using your SFTP (or SSH actually) connection.
Related question: How to decode Zip file from sftp file using paramiko python

Copy folder from server(Linux) to local machine(windows) in python

How to copy a folder from Server (linux) to a local machine (windows) in python.
I tried with the given code but it did not work
from distutils.dir_util import copy_tree
copy_tree("source_path ","destination_path")
I used copy_tree command to copy a folder on my local machine but when I used the same command to copy a folder from server to local machine then it did not work.
Any other method is there? Or any changes needed?
You need to use SSH, SCP, or SFTP to transfer files from host to host.
I do this a lot and like to use SSH and SCP. You can run and SSH server on your windows machine using OpenSSH. Here is a good set of instructions from WinSCP: https://winscp.net/eng/docs/guide_windows_openssh_server.
I recommend using Paramiko for SSH with Python. Here is a good answer showing how this works with python: https://stackoverflow.com/a/38556344/634627.
If you set up OpenSSH, you could also do this with SFTP, sometimes I find this is more suitable that SCP. Here is a good answer showing how that works: https://stackoverflow.com/a/33752662/634627
The trick is getting OpenSSH running on your Windows host and setting up SSH keys so your server can authenticate to your localhost.
Using copytree should work if:
the folder on the server is made available to the windows machine as a client.
you have sufficient access permissions.
you use a raw string for the windows path to prevent string interpretation.
Ad 3: try print('c:\test\robot'):
In [1]: print('c:\test\robot')
obot est

Categories