我使用的是Python 3.8.10和面料2.7.0。
我有一个连接
到远程主机。我正在执行以下命令:
resObj = connection.run("cat /usr/bin/binaryFile")
因此,从理论上讲,/usr/bin/binaryfile
的字节被泵送到 stdout
中,但我无法弄清楚需要什么向导。要将它们从 resobj.stdout
中脱颖而出,并将其写入本地的文件中,该文件将具有匹配的校验和,例如,将所有字节从Stdout中删除)。对于初学者, len(resobj.stdout)!== binaryfile.size
。从视觉上浏览 resobj.stdout
中的存在,并与/usr/bin/bin/binaryfile
通过 hexdump
或类似的内容进行比较关于类似,但出了问题。
愿唱片显示,我知道这个特定的示例将更好地完成...
connection.get('/usr/bin/binaryFile')
重点是我希望能够从Stdout中获取任意二进制数据。
任何帮助将不胜感激!!!
I am using Python 3.8.10 and fabric 2.7.0.
I have a Connection
to a remote host. I am executing a command such as follows:
resObj = connection.run("cat /usr/bin/binaryFile")
So in theory the bytes of /usr/bin/binaryFile
are getting pumped into stdout
but I can not figure out what wizardry is required to get them out of resObj.stdout
and written into a file locally that would have a matching checksum (as in, get all the bytes out of stdout). For starters, len(resObj.stdout) !== binaryFile.size
. Visually glancing at what is present in resObj.stdout
and comparing to what is in /usr/bin/binaryFile
via hexdump
or similar makes them seem about similar, but something is going wrong.
May the record show, I am aware that this particular example would be better accomplished with...
connection.get('/usr/bin/binaryFile')
The point though is that I'd like to be able to get arbitrary binary data out of stdout.
Any help would be greatly appreciated!!!
发布评论
评论(1)
我最终使用织物库放弃了这样做,并恢复了帕拉马科的直接。人们给Paramiko一个困难的时光是“太低的水平”,但事实是,它提供了更高级别的API,而使用非常直观。我最终得到了这样的事情:
在此设置中,我可以通过
stdout.read()
(或类似地,stderr.read()
)获得原始字节。要做其他揭露的事物,例如
put
和get
很容易做到:还能够通过 this so回答做:
recv_exit_status
的文档列出了一些值得一提的gotchas。 。对我来说,这个故事的寓意是,面料最终会感觉像是一个过度抽象,而帕拉马科(Paramiko)易于使用更高级别的API,并且在适当的情况下也具有低级原则。
I eventually gave up on doing this using the fabric library and reverted to straight up paramiko. People give paramiko a hard time for being "too low level" but the truth is that it offers a higher level API which is pretty intuitive to use. I ended up with something like this:
In this setup, I can get the raw bytes via
stdout.read()
(or similarly,stderr.read()
).To do other things that fabric exposes, like
put
andget
it is easy enough to do:also was able to get the exit code per this SO answer by doing:
The docs for
recv_exit_status
list a few gotchas that are worth being aware of too. https://docs.paramiko.org/en/latest/api/channel.html#paramiko.channel.Channel.recv_exit_status .Moral of the story for me is that fabric ends up feeling like an over abstraction while Paramiko has an easy to use higher level API and also the low level primitives when appropriate.