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 6185611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:44:42+00:00 2026-05-24T01:44:42+00:00

I want to write code in Ruby witch net::ssh that run commands one by

  • 0

I want to write code in Ruby witch net::ssh that run commands one by one on remote linux machine and log everything (called command, stdout and stderr on linux machine).

So I write function:

  def rs(ssh,cmds)
    cmds.each do |cmd|
      log.debug "[SSH>] #{cmd}"
      ssh.exec!(cmd) do |ch, stream, data|    
        log.debug "[SSH:#{stream}>] #{data}"            
      end  
    end
  end

For example if I want to create on remote linux new folders and file: “./verylongdirname/anotherlongdirname/a.txt”, and list files in that direcotry, and find firefox there (which is stupid a little :P) so i call above procedure like that:

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

  cmds=["mkdir verylongdirname", \                                 #1
        "cd verylongdirname; mkdir anotherlongdirname, \           #2
        "cd verylongdirname/anotherlongdirname; touch a.txt", \    #3
        "cd verylongdirname/anotherlongdirname; ls -la", \         #4
        "cd verylongdirname/anotherlongdirname; find ./ firefox"   #5 that command send error to stderr.
        ]

  rs(ssh,cmds)   # HERE we call our function

  ssh.loop
end

After run code above i will have full LOG witch informations about executions commands in line #1,#2,#3,#4,#5. The problem is that state on linux, between execude commands from cmds array, is not saved (so I must repeat “cd” statement before run proper command). And I’m not satisfy with that.

My purpose is to have cmds tables like that:

  cmds=["mkdir verylongdirname", \     #1
        "cd verylongdirname", \        
        "mkdir anotherlongdirname", \  #2
        "cd anotherlongdirname", \
        "touch a.txt", \               #3
        "ls -la", \                    #4
        "find ./ firefox"]             #5

As you see, te state between run each command is save on the linux machine (and we don’t need repeat apropriate “cd” statement before run proper command). How to change “rs(ssh,cmds)” procedure to do it and LOG EVERYTHING (comand,stdout,stdin) like before?

  • 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-05-24T01:44:43+00:00Added an answer on May 24, 2026 at 1:44 am

    Perhaps try it with an ssh channel instead to open a remote shell. That should preserve state between your commands as the connection will be kept open:

    http://net-ssh.github.com/ssh/v1/chapter-5.html

    Here’s also an article of doing something similar with a little bit different approach:

    http://drnicwilliams.com/2006/09/22/remote-shell-with-ruby/

    Edit 1:

    Ok. I see what you are saying. SyncShell was removed from Net::SSH 2.0. However I found this, which looks like it does pretty much what SyncShell did:

    http://net-ssh-telnet.rubyforge.org/

    Example:

    s = Net::SSH.start(host, user)
    t = Net::SSH::Telnet.new("Session" => s, "Prompt" => %r{^myprompt :})
    puts t.cmd("cd /tmp")  
    puts t.cmd("ls")       # <- Lists contents of /tmp
    

    I.e. Net::SSH::Telnet is synchronous, and preserves state, because it runs in a pty with your remote shell environment. Remember to set the correct prompt detection, otherwise Net::SSH::Telnet will appear to hang once you call it (it’s trying to find the prompt).

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

Sidebar

Related Questions

The code that I want to write is like this: void MethodOnThreadA() { for
I often find I want to write code something like this in C#, but
I am a AS3 novice learning PureMVC and want to write code following best
I'm overriding WndProc, so I want to write code like if (m.Msg == WM_COMMAND)
Is there a better way to write this code? I want to show a
I want to write a command that specifies the word under the cursor in
I am running a bit of code that looks like this: result = system(ruby
I was working with the lotrepls Ruby interpreter, and I want like to write
So, one commonly heard comment when talking about performance is that you write your
Not sure on my Ruby syntax here. I want to define a method that

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.