I’m trying to write a script that will establish an ssh tunnel through
“gatewaymachine.com” to a “targetmachine” on the other side. Sometimes
there’s already a tunnel established on the same port, in which case,
the command fails with an error message.
I’d like to be able to read the error message from the script. If the
script failed due to an incorrect port number, I’d like increment the
port number and try again.
Based on my google search, the popen3 command from the open3 library is
what I want to use.
But when I try to read from stderr, the read command never returns.
Here’s the code:
After I generated my own key file and ran your example (except with
gets) and with my key file, the system waited for a few Minutes and
generated this:
poplar% time ./x.rb
ssh: connect to host gatewaymachine.com port 22: Operation timed out
./x.rb 0.00s user 0.01s system 0% cpu 2:14.79 total
If nothing is able to be read from stderr, I suppose the read/gets
call will hang - you might want to put the read of stderr in a
thread. Something like this: