简体   繁体   English

使用pxssh / pexpect发送管道命令

[英]Sending piped commands with pxssh/pexpect

I am currently working with pxssh for Python and it's working like a charm, however it seems like it doesn't process piped (|) commands - it interprets them as separate ones. 我目前正在使用pxssh for Python,它的工作方式很吸引人,但是似乎它不处理管道(|)命令-它将它们解释为单独的命令。

At the moment I have the following: 目前,我有以下内容:

s = pxssh.pxssh()
s.login(sshHost, sshUsername, sshPassword)
s.sendline("cat somefile | grep something | cut -d \' -f 4")

It works properly with any commands that are not piped, however I need to send a piped one at the moment. 它可以与任何未通过管道传输的命令一起正常工作,但是我现在需要发送一个通过管道传输的命令。

Is there a way around this with pxssh, or can you please suggest a way to implement another solution for this command? 是否可以通过pxssh解决此问题,或者您可以建议一种方法来实现此命令的另一种解决方案?

It's not clear to me why pxssh would behave as you describe. 我不清楚为什么pxssh会像您描述的那样表现。 Are you sure the issue is not that your \\' is interpreted by Python, whereas you want it to be interpreted by the remote shell? 您确定问题不是Python解释了\\' ,而是您希望它被远程shell解释吗? That would be better spelled like so: 最好这样拼写:

s.sendline("cat somefile | grep something | cut -d \\' -f 4")

You certainly do have alternatives. 您当然有其他选择。 One would be to use a single command instead of a pipeline, such as: 一种方法是使用单个命令而不是管道,例如:

s.sendline("sed -n '/something/ { s/\\([^,]*,\\)\\{3\\}\\([^,]*\\),.*/\\2/; p }'")

As a special case of that, you could launch a subshell in which to run your pipeline: 作为一种特殊情况,您可以启动一个子外壳来运行管道:

s.sendline('''bash -c "cat somefile | grep something | cut -d \\' -f 4"''')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM