如何与python建立ssh连接? [英] How to make a ssh connection with python?

查看:53
本文介绍了如何与python建立ssh连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能推荐一些在python中建立ssh连接的东西?我需要它与任何操作系统兼容.

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

我已经尝试过 pyssh 只是为了得到 SIGCHLD 的错误,我读过这是因为 Windows 缺少这个.我曾尝试让 paramiko 工作,但我在 paramiko 和 Crypto 之间出现了错误,以至于它们的最新版本无法一起工作.

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 目前在 Windows 机器上.

Python 2.6.1 currently on a Windows machine.

推荐答案

注意这不会在 Windows 中工作.
模块 pxssh 完全符合您的要求:
例如,要运行 'ls -l' 并打印输出,您需要执行以下操作:

Notice that this doesn't work in Windows.
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 :

from pexpect 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()

一些链接:
Pxssh 文档:http://dsnra.jpl.nasa.gov/software/Python/site-packages/Contrib/pxssh.html
Pexpect(pxssh基于pexpect):http://pexpect.readthedocs.io/en/stable/

这篇关于如何与python建立ssh连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆