Python SFTP Script
This Python script uses paramiko to securely upload files from a specified local directory to an SFTP server. It establishes an SSH connection using a private key for authentication, iterates through
Key Points:
Dependencies:
The script relies on the
paramiko
module to handle SSH and SFTP connections and theos
module to interact with the local file system.
SFTP Connection Details:
It establishes an SFTP connection using SSH credentials. The username and host are hardcoded, while the private key file is used for authentication.
Local Directory:
The
local_directory
variable is set to point to the folder containing files that need to be uploaded. This path is customizable.
Functionality:
The script defines a function
sftp_upload_files
that:Initializes an SSH client.
Loads the private key from the specified file.
Connects to the remote SFTP server.
Opens an SFTP session.
Iterates over all the files in the specified local directory.
For each file, uploads it to the remote server.
After all files are uploaded, the SFTP and SSH sessions are closed.
Error Handling:
Errors related to SFTP connection issues or file uploads are caught and printed to the console. This ensures that if something goes wrong during the process, a message is displayed.
Example Workflow:
When the script is executed, it uploads each file from the local_directory directory to the remote server using the specified private key for authentication. For each successful upload, a confirmation message is printed. If any errors occur during the connection or upload process, they are caught and reported.
Last updated