Automating File Uploads and Downloads using Python with SSH Keys
This user guide provides instructions on how to use Python and the Paramiko library to upload and download files from the Sync platform using SFTP with both password authentication and SSH keys. The guide covers both file upload and download operations and explains how you can automate file transfers to and from the Sync platform using secure SFTP connections.
By using the Python methods provided in this guide, Sync users can automate the otherwise manual task of file transfers, helping streamline workflows and reducing the risk of errors. This guide also includes instructions for setting up authentication using SSH keys for added security.
What is Paramiko?
Paramiko is a popular Python library that allows secure SSH and SFTP connections. This guide uses Paramiko to facilitate secure file uploads and downloads to the Sync platform via SFTP. For more advanced information about Paramiko, including its installation and documentation, you can visit the official Paramiko documentation at: https://www.paramiko.org/.
Using SSH Keys for Authentication
Instead of using a password, you can authenticate with the Sync platform using SSH keys for added security. This requires generating a key pair (public and private keys) and adding the public key to the authorized keys on the server. The private key should be securely stored on your machine and used for authentication.
Below is an example of how to configure the Python script to use an SSH private key for authentication. Ensure that your SSH private key is stored safely and that you have access to it when running the script.
Downloading Files from Sync using Python with SSH Keys
To download a file from the Sync platform using SSH key authentication, use the following Python code. This script connects to the Sync platform, authenticates using the SSH private key, and downloads the specified file.
import paramiko
hostname = 'sync.sagacitysolutions.co.uk'
port = 22
username = 'your_username'
private_key_path = '/path/to/private/key' # Path to your private key file
remote_file_path = '/path/to/remote/file.txt'
local_file_path = '/path/to/local/file.txt'
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Load the private key
private_key = paramiko.RSAKey.from_private_key_file(private_key_path)
client.connect(hostname, port=port, username=username, pkey=private_key)
sftp = client.open_sftp()
try:
sftp.get(remote_file_path, local_file_path)
print("File downloaded successfully!")
except FileNotFoundError as e:
print(f"File not found: {e}")
except Exception as e:
print(f"An error occurred while downloading the file: {e}")
finally:
sftp.close()
except paramiko.AuthenticationException:
print("Authentication failed.")
except paramiko.SSHException as e:
print(f"Unable to establish SSH connection: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
client.close()
This script demonstrates how to securely authenticate with the Sync platform using an SSH private key and download files to your local machine.
Uploading Files to Sync using Python with SSH Keys
To upload a file to the Sync platform using SSH key authentication, the following Python script is used to establish a secure connection and transfer the file to the Sync platform.
import paramiko
hostname = 'sync.sagacitysolutions.co.uk'
port = 22
username = 'your_username'
private_key_path = '/path/to/private/key' # Path to your private key file
local_file_path = '/path/to/local/file.txt'
remote_file_path = '/path/to/remote/file.txt'
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Load the private key
private_key = paramiko.RSAKey.from_private_key_file(private_key_path)
client.connect(hostname, port=port, username=username, pkey=private_key)
sftp = client.open_sftp()
try:
sftp.put(local_file_path, remote_file_path)
print("File uploaded successfully!")
except FileNotFoundError as e:
print(f"File not found: {e}")
except Exception as e:
print(f"An error occurred while uploading the file: {e}")
finally:
sftp.close()
except paramiko.AuthenticationException:
print("Authentication failed.")
except paramiko.SSHException as e:
print(f"Unable to establish SSH connection: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
client.close()
This script handles uploading a file to the Sync platform securely using SSH key authentication.
Conclusion
Using SSH keys for authentication provides an additional layer of security for your automated file transfers with the Sync platform. By incorporating these Python scripts into your workflow, you can easily automate secure file uploads and downloads, reducing manual intervention and potential errors.