如何使用 pexpect 发送单个 ssh 命令以获取结果字符串? [英] How can I send single ssh command to get result string with pexpect?

查看:78
本文介绍了如何使用 pexpect 发送单个 ssh 命令以获取结果字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使我的代码尽可能高效和简单.我想让我的 pexpect 命令变成 1 行:

I am trying to make my code as efficient and simple as possible. I would like to make my pexpect command into 1 line:

当前命令(简化):

import pexpect
...
session=pexpect.spawn( 'ssh  %s@%s'%(un,ip), timeout)
session.expect(prompt, timeout)
session.sendline('ls')
session.expect(prompt)
print session.before

我想在我的 ssh 命令中完成这一切,但我返回一个 pexpect 对象,有没有办法只返回输出字符串?

I would like to do this all in my ssh command but I am returning a pexpect object, is there a way to return just the output string?

import pexpect
    ...
    print str(pexpect.spawn( 'ssh  %s@%s ls'%(un,ip), timeout))

有没有办法改变上面的代码才能工作?

Is there a possible way to change the code above to work?

我必须使用 pexpect 或标准 python 2.4,而不是 paramiko :(

I have to use pexpect or standard python 2.4, not paramiko :(

推荐答案

您想要 read() 方法:

p = pexpect.spawn('ssh  %s@%s ls'%(un,ip), timeout)
print(p.read())

这篇关于如何使用 pexpect 发送单个 ssh 命令以获取结果字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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