I've been looking around for a module which allows me to do SSH / SFTP functions in python without using POPEN to do it manually. Is there anything like this? I haven't found any real information on this, thanks!
4 Answers
For SFTP, you can use pysftp, which is a thin wrapper over paramiko's SFTPClient (pip install sftp).
Example to download a file:
import pysftp #pip install sftp
import sys
hostname = "128.65.45.12"
username = "bob"
password = "123456"
sftp = pysftp.Connection(hostname, username=username, password=password)
sftp.get('/data/word_vectors/GoogleNews-vectors-negative300.txt', preserve_mtime=True)
print('done')
2 Comments
John Y
I like the pysftp package, and I like that you noted that it's called
sftp when installing via pip, despite being imported as pysftp. However, I needed a new installation recently (for Python 3.6), and pip actually wants pysftp now. In addition, for better security, you now have to explicitly disable host key checking if you want to use password-only authentication. This isn't hard to do at all (a couple extra lines of code, and the warning message and docs spell out what you need to do), but you might want to update your answer to reflect these changes.Franck Dernoncourt
@JohnY thanks good to know. I am not using the package at the moment; if you have some code snippet you're most welcome to update the answer.
Depending on what you're looking to do over ssh, you might also benefit from looking at the pexpect library: http://www.noah.org/wiki/pexpect