Reputation: 77
I know how to redirect stdout to a file, but is it possible to redirect stdout to a process (linux environment)?
For example, if I move an active SSH session to the background via "~^Z", is there a way to then start another program on my local host and redirect its stdout to the SSH session?
Thanks!
Upvotes: 4
Views: 1645
Reputation: 30200
Sometimes a trick like
echo "something" > /proc/pid-of-process/fd/0
works, but if they're linked to pseudoterminals, it won't.
So, here's one way to do what you want.
mkfifo mypipe
)Use tail to read from the pipe and pass that to the SSH process, eg:
tail -f mypipe | ssh -t -t [email protected]
Send whatever you want to go into the ssh session into the named pipe, eg:
echo "ls -l" > mypipe
So if you need to pipe stuff from another program, you'd just do
./my-program > /path/to/mypipe
You're done.
Some notes:
-f
option to tail
is necessary to prevent the SSH process from receiving an EOF when the pipe is empty. There are other ways to prevent the pipe from closing, but this is the easiest in my opinion.-t -t
option to ssh
forces tty allocation, otherwise it would complain since stdin is being piped in your case. Upvotes: 3