Having trouble reading from stderr

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:

Advertisements

Guest

The fix is simple - replace 'read' with 'gets', ie, puts stderr.gets.

On Feb 25, 7:49 pm, Christopher Rasch <> wrote:
> Hi,
>
> 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:
>
> require 'open3'
>
> stdin, stdout, stderr = Open3.popen3("ssh -i
> /Users/crasch/.ssh/id_rsa.pub -L 2222:targetmachine:22 -N")
>
> puts stderr.read <---hangs indefinitely here
>
> Any suggestions for what might be happening? Thanks for any suggestions
> you may wish to provide.
>
> Chris

Could it be that stderr has received a partial error message, but won't write anything out until it receives a signal
that ssh process has ended?

Chris

wrote:
> The fix is simple - replace 'read' with 'gets', ie, puts stderr.gets.
>
>
>
> On Feb 25, 7:49 pm, Christopher Rasch <> wrote:
>> Hi,
>>
>> 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:
>>
>> require 'open3'
>>
>> stdin, stdout, stderr = Open3.popen3("ssh -i
>> /Users/crasch/.ssh/id_rsa.pub -L 2222:targetmachine:22 -N")
>>
>> puts stderr.read <---hangs indefinitely here
>>
>> Any suggestions for what might be happening? Thanks for any suggestions
>> you may wish to provide.
>>
>> Chris
>
>
>
>
>
>

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!