为什么 python 无法使用 tee 实用程序处理管道?
Why python unable to take care of pipe with tee utility?
我想 运行 从远程计算机中的 python 代码执行以下命令:
/bin/cat < abc.txt | tee >(echo $(md5sum) > /tmp/abc.md5sum) | some-other-command
.
我必须使用 Cumin 将此命令发送到远程机器。我的代码如下:
from multiprocessing import Pipe, Process
import os
import cumin
from cumin import query, transport, transports
from mypackage.RemoteExecution import CommandReturn, RemoteExecution
def run_subprocess(host, command, input_pipe):
e = CuminExecution()
result = e.run(host, command)
input_pipe.send(result)
class CuminExecution(RemoteExecution):
"""
RemoteExecution implementation using Cumin
"""
def __init__(self, options={}):
self._config = None
self.options = options
@property
def config(self):
if not self._config:
self._config = cumin.Config()
return self._config
def format_command(self, command):
if isinstance(command, str):
return command
else:
return ' '.join(command)
def run(self, host, command):
hosts = query.Query(self.config).execute(host)
if not hosts:
return CommandReturn(1, None, 'host is wrong or does not match rules')
target = transports.Target(hosts)
worker = transport.Transport.new(self.config, target)
worker.commands = [self.format_command(command)]
worker.handler = 'sync'
return_code = worker.execute()
for nodes, output in worker.get_results():
if host in nodes:
result = str(bytes(output), 'utf-8')
return CommandReturn(return_code, result, None)
return CommandReturn(return_code, None, None)
def start_job(self, host, command):
output_pipe, input_pipe = Pipe()
job = Process(target=run_subprocess, args=(host, command, input_pipe))
job.start()
input_pipe.close()
return {'process': job, 'pipe': output_pipe}
def monitor_job(self, host, job):
if job['process'].is_alive():
return CommandReturn(None, None, None)
else:
result = job['pipe'].recv()
job['pipe'].close()
return result
def kill_job(self, host, job):
if job['process'].is_alive():
job['process'].terminate()
def wait_job(self, host, job):
job['process'].join()
result = job['pipe'].recv()
job['pipe'].close()
return result
src_command = "/bin/cat < abc.txt | tee >(echo $(md5sum) > /tmp/abc.md5sum) | some-other-command"
result = CuminExecution.run(host, src_command)
当我从终端 运行 命令 /bin/cat < abc.txt | tee >(echo $(md5sum) > /tmp/abc.md5sum) | some-other-command
时,它按预期工作。但是当我尝试使用上面的代码使用 python3 运行 它时,它可以正确处理所有命令,除了写入 /tmp/abc.md5sum
部分!在 运行 之后,文件 abc.md5sum 包含不正确的散列。
有人可以帮我解决这个问题吗?
我把命令改成这样:
/bin/cat < abc.txt | tee >(md5sum > /tmp/abc.md5sum) | some-other-command
现在可以使用了!谢谢大家的帮助!
我想 运行 从远程计算机中的 python 代码执行以下命令:
/bin/cat < abc.txt | tee >(echo $(md5sum) > /tmp/abc.md5sum) | some-other-command
.
我必须使用 Cumin 将此命令发送到远程机器。我的代码如下:
from multiprocessing import Pipe, Process
import os
import cumin
from cumin import query, transport, transports
from mypackage.RemoteExecution import CommandReturn, RemoteExecution
def run_subprocess(host, command, input_pipe):
e = CuminExecution()
result = e.run(host, command)
input_pipe.send(result)
class CuminExecution(RemoteExecution):
"""
RemoteExecution implementation using Cumin
"""
def __init__(self, options={}):
self._config = None
self.options = options
@property
def config(self):
if not self._config:
self._config = cumin.Config()
return self._config
def format_command(self, command):
if isinstance(command, str):
return command
else:
return ' '.join(command)
def run(self, host, command):
hosts = query.Query(self.config).execute(host)
if not hosts:
return CommandReturn(1, None, 'host is wrong or does not match rules')
target = transports.Target(hosts)
worker = transport.Transport.new(self.config, target)
worker.commands = [self.format_command(command)]
worker.handler = 'sync'
return_code = worker.execute()
for nodes, output in worker.get_results():
if host in nodes:
result = str(bytes(output), 'utf-8')
return CommandReturn(return_code, result, None)
return CommandReturn(return_code, None, None)
def start_job(self, host, command):
output_pipe, input_pipe = Pipe()
job = Process(target=run_subprocess, args=(host, command, input_pipe))
job.start()
input_pipe.close()
return {'process': job, 'pipe': output_pipe}
def monitor_job(self, host, job):
if job['process'].is_alive():
return CommandReturn(None, None, None)
else:
result = job['pipe'].recv()
job['pipe'].close()
return result
def kill_job(self, host, job):
if job['process'].is_alive():
job['process'].terminate()
def wait_job(self, host, job):
job['process'].join()
result = job['pipe'].recv()
job['pipe'].close()
return result
src_command = "/bin/cat < abc.txt | tee >(echo $(md5sum) > /tmp/abc.md5sum) | some-other-command"
result = CuminExecution.run(host, src_command)
当我从终端 运行 命令 /bin/cat < abc.txt | tee >(echo $(md5sum) > /tmp/abc.md5sum) | some-other-command
时,它按预期工作。但是当我尝试使用上面的代码使用 python3 运行 它时,它可以正确处理所有命令,除了写入 /tmp/abc.md5sum
部分!在 运行 之后,文件 abc.md5sum 包含不正确的散列。
有人可以帮我解决这个问题吗?
我把命令改成这样:
/bin/cat < abc.txt | tee >(md5sum > /tmp/abc.md5sum) | some-other-command
现在可以使用了!谢谢大家的帮助!