11

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!

1

4 Answers 4

14

You're probably looking for the excellent paramiko library:

http://www.paramiko.org/

Sign up to request clarification or add additional context in comments.

Comments

4

paramiko works nicely: Paramiko Homepage

Comments

2

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

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.
@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.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.