如何从远程计算机获取控制台输出(ssh + python)
我用谷歌搜索了“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试过更简单的方法吗?
当然,这只有效,因为我已经使用远程主机知道的私钥预加载了
ssh-agent
运行。Have you tried an even simpler approach?
Granted, this only works because I have
ssh-agent
running preloaded with a private key that the remote host knows about.您可能还想研究 paramiko,它是另一个 Python SSH 库。
You might also want to investigate paramiko which is another SSH library for Python.
尝试发送
IIRC,您发送的文本将被逐字解释,因此另一台计算机可能正在等待您完成命令。
Try to send
IIRC, the text you send is interpreted verbatim, so the other computer is probably waiting for you to complete the command.