Python script to copy file and directory from one remote server to another 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.
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
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.
But that's not copying from one SFTP server to another SFTP server. That's copying from one SSH server to SFTP server. You need an SSH access, mere SFTP access is not enough.
To execute a command of a remote SSH server, use pysftp Connection.execute
. Though using pysftp to execute a command on a server is a bit overkill. You can use directly Paramiko instead:
Python Paramiko - Run command
(pysftp is just a wrapper around Paramiko with more advanced SFTP functions)
Copy a file to server with a different filename using Python Paramiko over ssh/sftp
So upload the file straight to the new name:
sftp.put("c:\file_a.txt", "/home/the_user/file_b.txt")
How do i copy a python file from my windows to Linux subsystem after connect ssh to my server?
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
scp between two remote servers in Python
You cannot write a password to the standard input of OpenSSH scp
.
Try it in a shell, it won't work either:
echo password | scp /home/oracle/myFile.txt oracle@10.10.10.20:/home/oracle/myFile.txt
OpenSSH tools (including scp
) read the password from a terminal only.
You can emulate the terminal by setting get_pty
parameter of SSHClient.exec_command
:
stdin, stdout, stderr = s.exec_command("scp ...", get_pty=True)
stdin.write('password\n')
stdin.flush()
Though enabling terminal emulation can bring you unwanted side effects.
A way better solution is to use a public key authentication. There also other workarounds. See How to pass password to scp? (though they internally have to do something similar to get_pty=True
anyway).
Other issues:
- You have to wait for the command to complete. Calling
s.close()
will likely terminate the transfer. Usingstdout.readlines()
will do in most cases. But it may hang, see Paramiko ssh die/hang with big output. - Do not use
AutoAddPolicy
– You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
Read file from remote server completely to local machine in python SSHCLient?
So here is what worked for me.
- since the file was too big to read via ssh client from remote server and I was only looking for file completion indication, which i can see only at EOF.
- my startegy was to connect to remote server and run 'tac' linux command- which reverse the file and then i accessed the reversed file and found the EOF results in beginning of file and i was able to confirm the Validation of file
code:
*SSH client connect to remote server
ssh.exec("tac" + filename.txt + " >> filereversed.txt" )
*read the filereversed.txt from remote server and validate
Related Topics
Xlsxwriter: How to Open an Existing Worksheet in My Workbook
Fillna in Multiple Columns in Place in Python Pandas
Saving Numpy Array to Txt File Row Wise
Change CSV Name to CSV Date Time Python
How to Merge Json Objects Containing Arrays Using Python
How to Determine If Current Time Is Within a Specified Range Using Python'S Datetime Module
Splitting Dictionary Items into Smaller Dictionaries Based on Condition
Use Cumcount on Pandas Dataframe With a Conditional Increment
Pandas Convert Columns to Percentages of the Totals
Cv2 Image Error: Error: (-215:Assertion Failed) !Ssize.Empty() in Function 'Cv::Resize'
Creating a New Dataframe Column by Comparing Strings of Two Unequal Dataframes
How to Check for an Exact Word in a String in Python
How to Stop Execution of All Cells in Jupyter Notebook
How to Make a Smiley Face in Turtle
Get Usb Device Address Through Python
Convenient Way to Handle Deeply Nested Dictionary in Python