amoffat / sh

Python process launching
https://sh.readthedocs.io/en/latest/
MIT License
6.98k stars 506 forks source link

piped commands via ssh? #713

Closed git-os closed 11 months ago

git-os commented 11 months ago
import sh
print(sh.awk("{print $5}", _in=sh.df("-h", "/")))

works fine but none of these seem to work

import sh
srv = sh.ssh("root@mysrv")
print(srv.awk("{print $5}", _in=sh.df("-h", "/")))
print(srv.awk("{print $5}", _in=srv.df("-h", "/")))

is this intended behaviour because it is not supported?

amoffat commented 11 months ago

Yes intended behavior. srv is a string (the return result of sh.ssh). What you're trying to do, as simple as it appears conceptually, is very advanced. You'll need to use a different library for running remote processes.

git-os commented 11 months ago

Got it, maybe you could give me advice about implementation. Currently I have a function, that pretty much gets a binary along with its args (including potentially included _in special kwargs), and then forms a baked command for execution at a later time. Could I use contrib wrapping somehow to maybe execute the first level (remote) Command and then have a follow up function passing the result of it to the _in defined command and execute it locally, then return the result?

ecederstrand commented 11 months ago

Keeping SSH connections open and running remote commands on a server is outside the scope of sh. It's not that it can't be done, but sh doesn't provide any help besides running commands locally and collecting the output. If you want to automate remote execution of commands, a tool like Fabric is a better choice.

git-os commented 11 months ago

ok, thanks