I have been searching for a way to run an interactive command (such as ssh
) and capture its output. This sounds simple but as I understand it, my use case is slightly different from a lot of the google and s/o results.
To be clear, by interactive I mean, the user who runs my_ruby_script.rb
from the terminal should be able to
- type into (
stdin
) the running interactive system command (e.g., typels
throughssh
tunnel) - read out from (
stdout
/stderr
) the running interactive system command (again e.g., seeing the output ofls
in anssh
tunnel)
On top of that, I would like to read the output of the system command (ssh
) so I may, for example, check for errors from inside my Ruby script. The closest that I have gotten has been to simply tee
the output to another file.
system('ssh username@ssh_server |& tee ssh_log.txt')
This is not ideal as
- It requires saving to/reading from a separate file
- It requires a more complicated system call which may or may not be portable
As I understand it, most of the solutions online (backticks, Open3.*
, PTY.spawn()
, etc. ) redirect stdin/stdout away from the terminal interface and into the ruby program. What I'm looking for is to leave stdin
alone, and tee stdout
to my Ruby script. Does anybody have any ideas?
I should note that system('ssh username@ssh_server')
does exactly what I want, I just need to be able to read the output. Additionally, I would prefer not to use net-ssh nor any other non-stdlib libraries.
Research:
- This is probably the most comprehensive page describing running commands in Ruby (When to use each method of launching a subprocess in Ruby and the linked blog posts).
- These people have (what seems to me) my exact same issue:
- These people are using ssh through Ruby but their scripts aren't "interactive"
I really appreciate any insight you can deliver.