I'm trying to set up an SFTP connection with Python (v2.7) using Paramiko's library http://docs.paramiko.org/en/2.1/api/sftp.html
From bits and pieces online I was able to (I think so anyway) connect using a private encrypted certificate.
This looks as follows:
import paramiko
sftp = paramiko.SSHClient()
hostname = "sftp.host.com"
port = 8022
username = "sftplogin"
k = paramiko.RSAKey.from_private_key_file("private.pem", password="XXX")
sftp.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "Connecting..."
sftp.connect(hostname, port, username=username, pkey=k )
print "Connected to: " + hostname + ":" + port
print sftp.getcwd()
sftp.close()
At this point, I'm just trying to see if I'm not delusional and in fact did not connect, so I'm attempting to print a getcwd() to get the current directory ... Sadly I'm getting nowhere, as it's returning this error:
AttributeError: 'SSHClient' object has no attribute 'getcwd'
Can anyone tell me why? What am I doing wrong?
Thanks in advance!
This has worked for me.
import os
import paramiko
host = "sftp.host.com"
port = 8022
transport = paramiko.Transport((host, port))
username = "sftplogin"
mykey = paramiko.RSAKey.from_private_key_file("private_export.pem", password="XXX")
print "Connecting..."
transport.connect(username = username, pkey = mykey)
sftp = paramiko.SFTPClient.from_transport(transport)
print "Connected."
print sftp.listdir()
sftp.close()
transport.close()
print "Closed connection."
You instantiated a paramiko.SSHClient
object, but you're trying to execute SFTP Client methods .
You should be able to execute this to demonstrate that fact:
sftp.exec_command('pwd')
But I think you meant to instantiate the SFTP class, rather than the SSH client, so that is probably where you went wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.