如何从Python Fabric Remote命令中恢复原始二进制输出?

发布于 2025-01-30 11:18:08 字数 700 浏览 4 评论 0 原文

我使用的是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!!!

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

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

发布评论

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

评论(1

深海少女心 2025-02-06 11:18:08

我最终使用织物库放弃了这样做,并恢复了帕拉马科的直接。人们给Paramiko一个困难的时光是“太低的水平”,但事实是,它提供了更高级别的API,而使用非常直观。我最终得到了这样的事情:

with SSHClient() as client:
  client.set_missing_host_key_policy(AutoAddPolicy())
  client.connect(hostname, **connectKwargs)
  stdin, stdout, stderr = client.exec_command("cat /usr/bin/binaryFile")

在此设置中,我可以通过 stdout.read()(或类似地, stderr.read())获得原始字节。

要做其他揭露的事物,例如 put get 很容易做到:

# client from above
with client.open_sftp() as sftpClient:
  sftpClient.put(...)
  sftpClient.get(...)

还能够通过 this so回答做:

# stdout from above
stdout.channel.recv_exit_status()

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:

with SSHClient() as client:
  client.set_missing_host_key_policy(AutoAddPolicy())
  client.connect(hostname, **connectKwargs)
  stdin, stdout, stderr = client.exec_command("cat /usr/bin/binaryFile")

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 and get it is easy enough to do:

# client from above
with client.open_sftp() as sftpClient:
  sftpClient.put(...)
  sftpClient.get(...)

also was able to get the exit code per this SO answer by doing:

# stdout from above
stdout.channel.recv_exit_status()

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.

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