Skip to main content
4 of 4
edited body
mikeserv
  • 59.4k
  • 10
  • 122
  • 242

What you want is already happening, actually. And certainly mkdir isn't your problem - it doesn't read stdin anyway. That pipe is inherited as stdin by all of the children of ssh - unless, that is, you are getting a pseudo terminal like ssh -t.

Barring that, then the problem is that the shell running those commands is one of ssh's children - and part of its job is to open 0,1,2 for each command group it runs. Your input, then, is just getting lost in the shuffle because it is on a common descriptor - the same descriptor that the shell reopens for every one of its children.

And so most simply, I think, you can just move the input to a descriptor that is other than one of those three defaults. For example, the following works for me:

echo hey there >file cat file | ssh mikeserv@localhost ' exec 3<&0 mkdir -p . echo ho there cat <&3' 

If I run the above it prints:

ho there hey there 

You might also find some utility in the -W option to ssh. man ssh says:

  • -W host :port - Requests that standard input and output on the client be forwarded to host on port over the secure channel. Implies -N, -T, ExitOnForwardFailure and ClearAllForwardings. Works with Protocol version 2 only
mikeserv
  • 59.4k
  • 10
  • 122
  • 242