Takkun Takkun - 23 days ago 15
Python Question

How to make a ssh connection with python?

Can anyone recommend something for making a ssh connection in python?
I need it to be compatible with any OS.

I've already tried pyssh only to get an error with SIGCHLD, which I've read is because Windows lacks this.
I've tried getting paramiko to work, but I've had errors between paramiko and Crypto to the point where the latest versions of each won't work together.

Python 2.6.1 currently on a Windows machine.

Answer

The module pxssh does exactly what you want.
For example, to run 'ls -l' and to print the output, you need to do something like that :

import pxssh
s = pxssh.pxssh()
if not s.login ('localhost', 'myusername', 'mypassword'):
    print "SSH session failed on login."
    print str(s)
else:
    print "SSH session login successful"
    s.sendline ('ls -l')
    s.prompt()         # match the prompt
    print s.before     # print everything before the prompt.
    s.logout()

Some links :
Pxssh docs : http://dsnra.jpl.nasa.gov/software/Python/site-packages/Contrib/pxssh.html
Pexpect (pxssh is based on pexpect) : http://www.noah.org/wiki/pexpect

Comments