Paramiko’s SSHClient with SFTP

Posted on

Question :

Paramiko’s SSHClient with SFTP

How I can make SFTP transport through SSHClient on the remote server? I have a local host and two remote hosts. Remote hosts are backup server and web server. I need to find on backup server necessary backup file and put it on web server over SFTP. How can I make Paramiko’s SFTP transport work with Paramiko’s SSHClient?

Asked By: Denis


Answer #1:


Sample Usage:

import paramiko

# Open a transport
host,port = "",22
transport = paramiko.Transport((host,port))

# Auth    
username,password = "bar","foo"

# Go!    
sftp = paramiko.SFTPClient.from_transport(transport)

# Download
filepath = "/etc/passwd"
localpath = "/home/remotepasswd"

# Upload
filepath = "/home/foo.jpg"
localpath = "/home/pony.jpg"

# Close
if sftp: sftp.close()
if transport: transport.close()
Answered By: leoluk

Answer #2:

If you have a SSHClient, you can also use open_sftp():

import paramiko

# lets say you have SSH client...
client = paramiko.SSHClient()

sftp = client.open_sftp()

# then you can use upload & download as shown above
Answered By: Alon Gouldman

Leave a Reply

Your email address will not be published.