Neekoy
Neekoy

Reputation: 2533

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.

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?

Upvotes: 1

Views: 566

Answers (1)

John Bollinger
John Bollinger

Reputation: 180093

It's not clear to me why pxssh would behave as you describe. Are you sure the issue is not that your \' is interpreted by Python, whereas you want it to be interpreted by the remote 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"''')

Upvotes: 1

Related Questions