Python : Navigating Inter-Server File Transfers in Unix with Python: A Hands-On Guide

python @ Freshers.in

In this tutorial, we’ll focus on copying a file called freshers_bkp_tbl.db from a source server to a specific location, /frehsers_in/bkp/daily/21072023/, on a destination server.

Requirements

To connect and transfer files between Unix servers using Python, you need the following:

  1. Python: Python 3 is installed on the source server. This article assumes familiarity with Python.
  2. SSH Access: Secure Shell (SSH) access to both source and destination servers. You’ll need the server address, username, and password for each.
  3. Paramiko: Paramiko is a Python implementation of SSHv2. We’ll use this for connecting to servers. It can be installed via pip:
pip install paramiko
  1. SCP: Secure Copy Protocol (SCP) for transferring files. This comes bundled with most Unix systems.
  2. Permissions: You have the required read/write permissions to access the file on the source server and the directory on the destination server.

Python Script for File Transfer

Let’s create a Python script using Paramiko’s SSH client to connect to the servers and transfer the file:

import paramiko
def transfer_file(src_path, dest_path, dest_server, dest_username, dest_password):
    # initialize the SSH client
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        # connect to the destination server
        ssh.connect(dest_server, username=dest_username, password=dest_password)
        # SCP command to transfer file
        scp_cmd = f'scp {src_path} {dest_username}@{dest_server}:{dest_path}'
        
        # execute the SCP command
        stdin, stdout, stderr = ssh.exec_command(scp_cmd)
        
        # print any output from the command
        for line in stdout:
            print('... ' + line.strip('\n'))
        print(f"File transferred successfully to {dest_server}:{dest_path}")
    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        # close the connection
        ssh.close()

# specify your parameters here
source_file = '/path/to/freshers_bkp_tbl.db'
destination_folder = '/frehsers_in/bkp/daily/21072023/'
destination_server = 'dest.server.com'
destination_username = 'username'
destination_password = 'password'

transfer_file(source_file, destination_folder, destination_server, destination_username, destination_password)

Explanation

  1. Importing the library: We import the Paramiko module, which we’ll use to establish the SSH connection.
  2. Defining the function: transfer_file is a function that connects to the destination server and copies the file from the source server to a specific destination path.
  3. Setting up the SSH client: We initialize the SSH client and set a policy to automatically add the server’s host key (this is necessary for the client to connect to the server).
  4. Connecting to the server: We use the connect method of the SSH client, passing it the server address, username, and password.
  5. Formulating the SCP command: We create a command to transfer the file. This command is a string that we’ll pass to the exec_command method.
  6. Executing the SCP command: We use the exec_command method of the SSH client to execute the SCP command. This method returns three streams – stdin, stdout, and stderr.
  7. Handling errors: If anything goes wrong during the connection or file transfer, we catch the exception and print an error message.
  8. Closing the connection: Regardless of whether the file transfer was successful or an error occurred, we ensure that the SSH connection is closed using the close method.
  9. Remember to replace the placeholders in the source_file, destination_folder, destination_server, destination_username, and destination_password variables with your actual details. This script will then transfer freshers_bkp_tbl.db to the /frehsers_in/bkp/daily/21072023/ directory on the destination server.
Refer more on python here :
Author: user

Leave a Reply