如何从远程计算机获取控制台输出(ssh + python)

发布于 2024-08-02 17:19:33 字数 481 浏览 7 评论 0原文

我用谷歌搜索了“python ssh”。有一个很棒的模块 pexpect,它可以使用以下命令访问远程计算机ssh(带密码)。

连接远程计算机后,我可以执行其他命令。但是我无法再次在 python 中得到结果。

p = pexpect.spawn("ssh user@remote_computer")
print "connecting..."
p.waitnoecho()
p.sendline(my_password)
print "connected"
p.sendline("ps -ef")
p.expect(pexpect.EOF) # this will take very long time
print p.before

在我的例子中如何获得 ps -ef 的结果?

I have googled "python ssh". There is a wonderful module pexpect, which can access a remote computer using ssh (with password).

After the remote computer is connected, I can execute other commands. However I cannot get the result in python again.

p = pexpect.spawn("ssh user@remote_computer")
print "connecting..."
p.waitnoecho()
p.sendline(my_password)
print "connected"
p.sendline("ps -ef")
p.expect(pexpect.EOF) # this will take very long time
print p.before

How to get the result of ps -ef in my case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风铃鹿 2024-08-09 17:19:34

您尝试过更简单的方法吗?

>>> from subprocess import Popen, PIPE
>>> stdout, stderr = Popen(['ssh', 'user@remote_computer', 'ps -ef'],
...                        stdout=PIPE).communicate()
>>> print(stdout)

当然,这只有效,因为我已经使用远程主机知道的私钥预加载了 ssh-agent 运行。

Have you tried an even simpler approach?

>>> from subprocess import Popen, PIPE
>>> stdout, stderr = Popen(['ssh', 'user@remote_computer', 'ps -ef'],
...                        stdout=PIPE).communicate()
>>> print(stdout)

Granted, this only works because I have ssh-agent running preloaded with a private key that the remote host knows about.

猛虎独行 2024-08-09 17:19:34
child = pexpect.spawn("ssh user@remote_computer ps -ef")
print "connecting..."
i = child.expect(['user@remote_computer\'s password:'])
child.sendline(user_password)
i = child.expect([' .*']) #or use i = child.expect([pexpect.EOF])
if i == 0:
    print child.after # uncomment when using [' .*'] pattern
    #print child.before # uncomment when using EOF pattern
else:
    print "Unable to capture output"


Hope this help..
child = pexpect.spawn("ssh user@remote_computer ps -ef")
print "connecting..."
i = child.expect(['user@remote_computer\'s password:'])
child.sendline(user_password)
i = child.expect([' .*']) #or use i = child.expect([pexpect.EOF])
if i == 0:
    print child.after # uncomment when using [' .*'] pattern
    #print child.before # uncomment when using EOF pattern
else:
    print "Unable to capture output"


Hope this help..
酒浓于脸红 2024-08-09 17:19:34

您可能还想研究 paramiko,它是另一个 Python SSH 库。

You might also want to investigate paramiko which is another SSH library for Python.

如此安好 2024-08-09 17:19:34

尝试发送

p.sendline("ps -ef\n")

IIRC,您发送的文本将被逐字解释,因此另一台计算机可能正在等待您完成命令。

Try to send

p.sendline("ps -ef\n")

IIRC, the text you send is interpreted verbatim, so the other computer is probably waiting for you to complete the command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文