Python Paramiko 执行命令后如何保存结果? [英] After executing a command by Python Paramiko how could I save result?

查看:67
本文介绍了Python Paramiko 执行命令后如何保存结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下所示,是否可以保存结果?因为,在第二个和第三个 stdout.read() 我无法达到结果.

As you see below, is it possible to save the result? Cause, at second and third stdout.read() I couldn't reach the result.

import paramiko
import os
dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
dssh.connect('192.168.1.250', username='root', password='pass')
import os
stdin, stdout, stderr = dssh.exec_command('ifconfig')
print stdout.read()
print ('Sleeping 2 seconds!')
os.system('sleep 2')
stdin, stdout, stderr = dssh.exec_command('ls -l')
print stdout.read()
print stdout.read()
print stdout.read()
dssh.close()

推荐答案

想象一下 stdout 是一个普通文件.如果您第二次调用 file.read(),您希望得到什么?-- 没有(空字符串),除非文件在外面发生了变化.

Imagine that stdout is an ordinary file. What do you expect to get if you call file.read() the second time? -- nothing (empty string) unless the file has changed outside.

保存字符串:

output = stdout.read()

您可能会发现 Fabric 使用起来更简单(它使用 paramiko 在后台执行命令).

You might find Fabric simpler to use (it uses paramiko to execute commands under the hood).

这篇关于Python Paramiko 执行命令后如何保存结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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