CertGrid CertGrid
Hands-on Lab·Python Automation for IT

Python SFTP File Transfers

`open_sftp()` on a connected client gives you a full filesystem API over the same connection - put, get, stat, listdir, and an `open()` that reads a remote file with no local copy. The parts worth knowing are the ones that are not obvious: `confirm=True` is a size check and not a checksum, an upload is only atomic if you rename it, there is no `walk` or `rmtree`, and half the errors arrive with no errno at all.

Remote Hosts and CI/CD Guide 33 of 39 Intermediate

Written against the versions above. paramiko 5.0.0. `posix_rename` needs the server to support the `posix-rename@openssh.com` extension, which OpenSSH has had for many years; the fallback matters for other servers.

One control node and two managed hosts. The targets keep the hostnames the RHCSA path gave them - they are ordinary Linux boxes reachable over SSH, and nothing here is Red Hat specific.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB
RHCSA-A01192.168.0.31RHEL 10.0Managed host - reached over SSH from the control node2 Core4 GB50 GB
RHCSA-B01192.168.0.33RHEL 10.0Second managed host - so an inventory has more than one row2 Core4 GB50 GB

Before you start

  1. A file up, and the same file back

  2. What confirm= is actually doing

  3. A partial upload, and why the destination name matters

  4. Listing, statting and reading in place

  5. Walking a remote tree, which SFTP does not do for you

  6. The errors, and the ones with no errno at all

  7. A progress callback

  8. And the same job with scp, for comparison

  9. Cleaning up, on both ends

Official sources