【问题标题】:twisted run local shell commands with pipeline使用管道扭曲运行本地 shell 命令
【发布时间】:2015-03-10 06:16:32
【问题描述】:

在 twisted 中,getProcessOutput 方法可以通过使用 getProcessOutupt('ps', 'aux') 获取 'ps' shell 命令输出并返回延迟。

我的问题是如何在 getProcessOutput 中运行类似“ps aux | grep 'some keyword' | awk '{...}'”的命令。例如 getProcessOutput("ps aux | grep 'some keyword' | awk '{...}'").

任何建议将不胜感激。

【问题讨论】:

  • 一个非常丑陋的技巧是将整个命令管道写入一个适合作为单个参数传递给sh -c "..." 的字符串。那么您的调用将类似于: getProcessOutput('sh', ('-c', "ps aux | grep 'some keyword' | awk '{...}'"), *other_getProd_args)
  • @JimDennis,感谢您的回答。 :-) 。我想通过使用透视代理服务器从一些远程计算机获取命令输出,该代理服务器使用 getProcessOutput 运行本地 shell 命令。你对最好的方法有什么想法吗?
  • 如果你真的想要 shell 管道,["/bin/sh", "-c", "..."] 并不是一个丑陋的 hack,它是正确的方法。
  • 如果您想要从远程计算机获取输出并将其发送到本地程序,您想要实现def remote_something(self, some_bytes) 以在与关联的ProcessTransport 上调用write ProcessProtocol 你是使用 spawnProcess 生成的。如果你想问一个关于透视代理的问题,它不是关于 shell 管道的,所以请提出一个单独的问题 :)

标签: python twisted twisted.internet


【解决方案1】:

使用getProcessOutput('/bin/sh', ('-c', cmd))。 cmd 是你的 shell 命令。试试看:-)

【讨论】:

    最近更新 更多