6

如何在 paramiko 中运行管道命令?我正在这样做:

statement = 'grep thing file | grep thing2 | tail -1'
last_msg = conn.execute(statement)

我只得到输出grep thing file

4

1 回答 1

9

因为grep不知道怎么处理|。准备好进行一些令人讨厌的逃跑:

statement = """sh -c 'grep thing file | grep thing2 | tail -1'"""

这会在另一侧创建一个 shell,并要求它解释 string grep thing file | grep thing2 | tail -1。单引号是必需的,因为sh -c只接受一个参数。

这样,shell 将为您创建管道,运行所有命令。你最好确保文件名file不包含空格。如果是这样,请尝试"file"

如您所见,这很快变得非常难看。我建议您将管道放入 shell 脚本中。然后您可以避免使用引号,只需使用sh -c script.sh.

于 2011-01-04T09:23:12.907 回答