- 1 year ago
Remote to remote large file copy with python
To perform a remote-to-remote large file copy using Python, you can utilize SSH and SFTP protocols to establish connections and transfer the file between the remote systems. One option is to use the paramiko
library, which provides an SSH implementation in Python.
Here's an example that demonstrates how to copy a large file from one remote server to another using paramiko
:
pythonimport paramiko
# Source remote server details
source_host = 'source_server'
source_username = 'source_username'
source_password = 'source_password'
source_file = '/path/to/source/file.txt'
# Destination remote server details
destination_host = 'destination_server'
destination_username = 'destination_username'
destination_password = 'destination_password'
destination_file = '/path/to/destination/file.txt'
# Establish SSH connections to the remote servers
source_ssh = paramiko.SSHClient()
source_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
source_ssh.connect(source_host, username=source_username, password=source_password)
destination_ssh = paramiko.SSHClient()
destination_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
destination_ssh.connect(destination_host, username=destination_username, password=destination_password)
# Open SFTP sessions on the SSH connections
source_sftp = source_ssh.open_sftp()
destination_sftp = destination_ssh.open_sftp()
# Transfer the file from source to destination
source_sftp.get(source_file, destination_file)
# Close the SFTP sessions and SSH connections
source_sftp.close()
destination_sftp.close()
source_ssh.close()
destination_ssh.close()
Make sure to replace the placeholders 'source_server'
, 'source_username'
, 'source_password'
, 'destination_server'
, 'destination_username'
, and 'destination_password'
with the actual server details. Also, update the source_file
and destination_file
variables with the paths of the source and destination files, respectively.
The code establishes SSH connections to the source and destination servers, opens SFTP sessions on the connections, and performs the file transfer using the get()
method of the source SFTP session.
By utilizing the paramiko
library, you can copy large files between remote servers securely and efficiently.