Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8106963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:37:22+00:00 2026-06-06T00:37:22+00:00

How do I get stdout from the .execute method in Net-SSH-Shell With good ‘ol

  • 0

How do I get stdout from the .execute method in Net-SSH-Shell

With good ‘ol Net-SSH, this is easy

Net::SSH.start('host','user') do |ssh|
     puts ssh.exec! 'date'
end

Gives me Tue Jun 19 23:43:53 EDT 2012

but if I try to use a shell, I get a process object

Net::SSH.start('host','user') do |ssh|
    ssh.shell do |bash|
        output = bash.execute! 'ls'
        puts output
    end
end

Gives me #<Net::SSH::Shell::Process:0x007fc809b541d0>

I can’t find anything in the sparse docs about how to get standard out easily. There is the on_output method, but that doesn’t seem to do anything for me if I use the execute! method

I’ve also tried passing a block to .execute! like

bash.execute! 'ls' do |output|
    puts output
end

but I still get a process #<Net::SSH::Shell::Process:0x007fc809b541d0>

I need the standard out in a variable that I can use and I need an actual stateful login shell, which barebones Net-SSH doesn’t do to my knowledge.

Any ideas?

Edit

Along the same ideas of @vikhyat‘s suggestion, I tried

 ssh.shell do |bash|
      process = bash.execute! 'ls'  do |a,b|
            # a is the process itself, b is the output
            puts [a,b]
            output = b
      end
 end

But b is always empty, even when I know the command returns results.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:37:23+00:00Added an answer on June 6, 2026 at 12:37 am

    Have you tried doing it this way?

    Net::SSH.start('host','user') do |ssh|
        ssh.shell do |bash|
            process = bash.execute! 'ls'
            process.on_output do |a, b|
              # a is the process itself, b is the output
              p [a, b]
            end
        end
    end
    

    You can have a look at the definition of Net::SSH::Process here: https://github.com/mitchellh/net-ssh-shell/blob/master/lib/net/ssh/shell/process.rb

    EDIT

    I think the problem lies with the ! in execute! because the following works fine for me:

    require 'net/ssh'
    require 'net/ssh/shell'
    
    Net::SSH.start('students.iitmandi.ac.in', 'k_vikhyat') do |ssh|
      ssh.shell do |bash|
        process = bash.execute 'whoami'
        process.on_output do |a, b|
          p b
        end
        bash.wait!
        bash.execute! 'exit'
      end
    end
    

    I am not sure why this is the case, because it looks like execute! creates a process, runs wait! and then returns the process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP's proc_open() , I can start a process, read from STDOUT and STDERR
To launch programs from my Python-scripts, I'm using the following method: def execute(command): process
I am trying to execute remote commands from within a php script over ssh,
How do I execute a command-line program from C# and get back the STD
I have finally worked out how to get stdin and stdout to pipe between
Get class from div inside an li and add to the same li. The
GET is a convenient method to post the form id, post the website id
I want to execute an external command from within my Perl script, putting the
I am trying to execute a batch file using the Process class. This code
I'm at a loss debugging this code. I copied the example from a guide,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.