0

As the net-ssh manual book says the method "exec" executes a command asynchronously on channel.http://net-ssh.github.io/ssh/v1/chapter-3.html#s6

But when I hope to execute some instructors in order, for example:

def work(session, instructor )
  session.open_channel do |channel|
    channel.on_data do |ch, data|
      puts data.strip
    end
    channel.exec instructor
  end
end

Net::SSH.start( 'host' ) do |session|
  work session, "touch a.file" 
  work session, "mv a.file b.file"
  work session, "rm b.file"
  session.loop
end

Executing a command asynchronously really makes such code doesn't work.

Can anybody help me to solve this problem?

Thanks!

1 Answer 1

1

Use sync shell service for it, do something like:

Net::SSH.start( 'localhost' ) do |session|
  shell = session.shell.sync
  shell.touch 'a.file'
  shell.mv 'a.file', 'b.file'
  shell.send_command("rm", 'b.file')
  shell.exit
end

or just organize the commands on-a-line:

work session, "touch a.file; mv a.file b.file; rm b.file"
Sign up to request clarification or add additional context in comments.

3 Comments

What if the command I want to exec is not a basic shell command? I think the second way is OK. But it is not clear if my commands is very long. Thanks.
@wonderflow the document say not that the shell is only bash, I believe that is just the default remove shell.
@МалъСкрылевъ - I have a small issue with ssh connection state. Please help me - stackoverflow.com/questions/28799593/… thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.