Net::SSH - request a pty seems to put stderr in stdout

Discussion in 'Ruby' started by Najati Imam, Mar 31, 2010.

  1. Najati Imam

    Najati Imam Guest

    Hi all,

    As context, we're simply trying to remotely (automatically) "su" to a
    different user and issue commands as that user, while still being able
    to differentiate between stderr and stdout on the subsequent commands.
    If there's a common pattern for doing this, then answering my specific
    question isn't all that important (to me). Thanks!

    So, I'm trying to use Net::SSH to do some remote automation and when we
    need to su to do something, thus requiring a pty, it seems that the
    on_extended_data doesn't get called on stderr and that it, instead, gets
    mashed into on_data:

    Example:
    - - - - -
    require 'rubygems'
    require 'net/ssh'

    command = "cat slartibartfast"

    Net::SSH.start(host, user, :password => password) do |ssh|
    channel = ssh.open_channel do |ch|
    channel.request_pty do |ch, success|
    raise "no pty!" if !success
    end

    ch.exec command do |ch, success|
    raise "could not execute command" unless success

    password_sent = false
    ch.on_data do |c, data|
    puts "stdout: " + data
    end

    ch.on_extended_data do |c, type, data|
    puts "stderr: " + data
    end
    end
    end
    channel.wait
    end
    - - - - -

    Output:
    - - - - -
    stdout: cat: cannot open slartibartfast
    stdout:
    - - - - -

    If I take the request for the pty out, everything behaves as expected.
    Is this intentional behavior? I have yet to dig into the source, but
    will tomorrow.

    Thanks again!
    Najati
    --
    Posted via http://www.ruby-forum.com/.
     
    Najati Imam, Mar 31, 2010
    #1
    1. Advertising

  2. Najati Imam

    Guest

    On Wednesday, March 31, 2010 11:14:17 AM UTC-6, Najati Imam wrote:
    > Hi all,
    >
    > As context, we're simply trying to remotely (automatically) "su" to a
    > different user and issue commands as that user, while still being able
    > to differentiate between stderr and stdout on the subsequent commands.
    > If there's a common pattern for doing this, then answering my specific
    > question isn't all that important (to me). Thanks!
    >
    > So, I'm trying to use Net::SSH to do some remote automation and when we
    > need to su to do something, thus requiring a pty, it seems that the
    > on_extended_data doesn't get called on stderr and that it, instead, gets
    > mashed into on_data:
    >
    > Example:
    > - - - - -
    > require 'rubygems'
    > require 'net/ssh'
    >
    > command = "cat slartibartfast"
    >
    > Net::SSH.start(host, user, :password => password) do |ssh|
    > channel = ssh.open_channel do |ch|
    > channel.request_pty do |ch, success|
    > raise "no pty!" if !success
    > end
    >
    > ch.exec command do |ch, success|
    > raise "could not execute command" unless success
    >
    > password_sent = false
    > ch.on_data do |c, data|
    > puts "stdout: " + data
    > end
    >
    > ch.on_extended_data do |c, type, data|
    > puts "stderr: " + data
    > end
    > end
    > end
    > channel.wait
    > end
    > - - - - -
    >
    > Output:
    > - - - - -
    > stdout: cat: cannot open slartibartfast
    > stdout:
    > - - - - -
    >
    > If I take the request for the pty out, everything behaves as expected.
    > Is this intentional behavior? I have yet to dig into the source, but
    > will tomorrow.
    >
    > Thanks again!
    > Najati
    > --
    > Posted via http://www.ruby-forum.com/.


    I know this is from years ago, but I'm having the same problem, did anyone happen to find a solution to this?
     
    , Jun 24, 2012
    #2
    1. Advertising

  3. Najati Imam

    Guest

    On Saturday, June 23, 2012 9:02:15 PM UTC-4, wrote:
    > On Wednesday, March 31, 2010 11:14:17 AM UTC-6, Najati Imam wrote:
    >
    > > Hi all,

    >
    > >

    >
    > > As context, we're simply trying to remotely (automatically) "su" to a

    >
    > > different user and issue commands as that user, while still being able

    >
    > > to differentiate between stderr and stdout on the subsequent commands.

    >
    > > If there's a common pattern for doing this, then answering my specific

    >
    > > question isn't all that important (to me). Thanks!

    >
    > >

    >
    > > So, I'm trying to use Net::SSH to do some remote automation and when we

    >
    > > need to su to do something, thus requiring a pty, it seems that the

    >
    > > on_extended_data doesn't get called on stderr and that it, instead, gets

    >
    > > mashed into on_data:

    >
    > >

    >
    > > Example:

    >
    > > - - - - -

    >
    > > require 'rubygems'

    >
    > > require 'net/ssh'

    >
    > >

    >
    > > command = "cat slartibartfast"

    >
    > >

    >
    > > Net::SSH.start(host, user, :password => password) do |ssh|

    >
    > > channel = ssh.open_channel do |ch|

    >
    > > channel.request_pty do |ch, success|

    >
    > > raise "no pty!" if !success

    >
    > > end

    >
    > >

    >
    > > ch.exec command do |ch, success|

    >
    > > raise "could not execute command" unless success

    >
    > >

    >
    > > password_sent = false

    >
    > > ch.on_data do |c, data|

    >
    > > puts "stdout: " + data

    >
    > > end

    >
    > >

    >
    > > ch.on_extended_data do |c, type, data|

    >
    > > puts "stderr: " + data

    >
    > > end

    >
    > > end

    >
    > > end

    >
    > > channel.wait

    >
    > > end

    >
    > > - - - - -

    >
    > >

    >
    > > Output:

    >
    > > - - - - -

    >
    > > stdout: cat: cannot open slartibartfast

    >
    > > stdout:

    >
    > > - - - - -

    >
    > >

    >
    > > If I take the request for the pty out, everything behaves as expected.

    >
    > > Is this intentional behavior? I have yet to dig into the source, but

    >
    > > will tomorrow.

    >
    > >

    >
    > > Thanks again!

    >
    > > Najati

    >
    > > --

    >
    > > Posted via http://www.ruby-forum.com/.

    >
    >
    >
    > I know this is from years ago, but I'm having the same problem, did anyone happen to find a solution to this?


    check this thread - http://stackoverflow.com/questions/...d-data-with-ssh-using-twisted-conch-as-client
     
    , Feb 13, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Donn Cave
    Replies:
    0
    Views:
    404
    Donn Cave
    Apr 11, 2008
  2. Evan Driscoll
    Replies:
    0
    Views:
    216
    Evan Driscoll
    Oct 24, 2012
  3. Evan Driscoll
    Replies:
    0
    Views:
    168
    Evan Driscoll
    Oct 24, 2012
  4. Evan Driscoll
    Replies:
    0
    Views:
    195
    Evan Driscoll
    Oct 24, 2012
  5. David Hutto
    Replies:
    0
    Views:
    205
    David Hutto
    Oct 24, 2012
Loading...

Share This Page