How to Transfer Files Between Local Machine and Remote Server Using sshpass in Python

Transferring files between a local machine and a remote server is a common task in software development, data science, and IT operations. One way to transfer files securely is to use Secure Shell (SSH), a cryptographic network protocol that allows you to access and manage remote systems securely over an unsecured network. In this article, we will discuss how to transfer files between a local machine and a remote server using sshpass in Python.

What is sshpass?

SSH is a powerful tool that enables secure remote access and management of servers. However, one of its drawbacks is that it requires you to enter a password every time you connect to a remote server. This can be cumbersome, especially when you need to automate repetitive tasks that involve multiple servers. To solve this problem, you can use sshpass, a utility that allows you to pass a password to SSH non-interactively.

sshpass is not installed by default on most systems. You can install it using your system's package manager or download the source code from the official website.

Transferring files from a local machine to a remote server

To transfer files from a local machine to a remote server using sshpass, you can use the scp command, which stands for "secure copy". scp allows you to securely copy files between a local machine and a remote server over an SSH connection. Here's how you can transfer a file from a local machine to a remote server using scp and sshpass in Python:

 1
 2import subprocess
 3
 4# Define the path to the file you want to transfer
 5local_file_path = "/path/to/local/file.txt"
 6
 7# Define the destination path on the remote server
 8remote_file_path = "/path/on/remote/server/file.txt"
 9
10# Define the remote server details
11remote_server = "remote.example.com"
12remote_username = "username"
13remote_password = "password"
14
15# Construct the sshpass command to transfer the file
16sshpass_command = [
17    "sshpass",
18    "-p",
19    remote_password,
20    "scp",
21    "-o",
22    "StrictHostKeyChecking=no",
23    f"{local_file_path}",
24    f"{remote_username}@{remote_server}:{remote_file_path}",
25]
26
27# Run the sshpass command to transfer the file
28subprocess.run(sshpass_command, check=True)

This program uses the subprocess module to execute the sshpass command with the appropriate arguments. The 'check=True' parameter is passed to 'subprocess.run' to raise an exception if the command fails.

Make sure to replace the placeholders for the local file path, remote file path, remote server details, and SSH password with the appropriate values for your use case.

Transferring files from a remote server to a local machine

To transfer files from a remote server to a local machine using sshpass, you can use the scp command with the -r option, which stands for "recursive". scp -r allows you to securely copy directories and their contents between a remote server and a local machine over an SSH connection. Here's how you can transfer a file from a remote server to a local machine using scp -r and sshpass in Python:

 1import subprocess
 2
 3# Define the path to the file you want to transfer
 4remote_file_path = "/path/on/remote/server/file.txt"
 5
 6# Define the destination path on the local machine
 7local_file_path = "/path/to/local/file.txt"
 8
 9# Define the remote server details
10remote_server = "remote.example.com"
11remote_username = "username"
12remote_password = "password"
13
14# Construct the sshpass command to transfer the file
15sshpass_command = [
16    "sshpass",
17    "-p",
18    remote_password,
19    "scp",
20    "-o",
21    "StrictHostKeyChecking=no",
22    f"{remote_username}@{remote_server}:{remote_file_path}",
23    f"{local_file_path}",
24]
25
26# Run the sshpass command to transfer the file
27subprocess.run(sshpass_command, check=True)

This program uses the subprocess module to execute the sshpass command with the appropriate arguments. The 'check=True' parameter is passed to 'subprocess.run' to raise an exception if the command fails.

Make sure to replace the placeholders for the local file path, remote file path, remote server details, and SSH password with the appropriate values for your use case.

Conclusion

In this article, we discussed how to transfer files between a local machine and a remote server using sshpass and scp in Python. sshpass allows you to pass a password to SSH non-interactively, which can be useful when you need to automate repetitive tasks that involve multiple servers. scp allows you to securely copy files between a local machine and a remote server over an SSH connection. By combining these two tools, you can easily transfer files between a local machine and a remote server in a secure and automated way.

I :heart: AWS! :smile: Enjoy