在paramiko管道命令 [英] Piped commands in paramiko
问题描述
我如何在paramiko中运行管道命令?我这样做: - $ / b>
$ b
statement ='grep thing file | grep thing2 | tail -1'
last_msg = conn.execute(statement)
grep thing file
only。
因为 grep
不知道如何处理 |
。准备好一些讨厌的转义:
statement =sh -c'grep thing file | grep thing2 | tail -1 ''
这会在另一面创建一个shell,并要求它解释字符串 grep thing文件| grep thing2 |尾-1
。单引号是必须的,因为 sh -c
只接受一个参数。
这样,shell将创建为你的管道,运行所有的命令。最好确保文件名 file
不包含空格。如果是,请尝试file
。
How do i run piped commands in paramiko? i'm doing this :-
statement = 'grep thing file | grep thing2 | tail -1'
last_msg = conn.execute(statement)
i get the output of grep thing file
only.
Because grep
doesn't know how to handle |
. Get ready for some nasty escaping:
statement = """sh -c 'grep thing file | grep thing2 | tail -1'"""
This creates a shell on the other side, and asks it to interpret the string grep thing file | grep thing2 | tail -1
. The single quotes are necessary since sh -c
accepts only a single argument.
That way, a shell will create the pipe for you, running all the commands. And you better be sure that the filename file
doesn't contain spaces. If it does, try "file"
.
这篇关于在paramiko管道命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!