2

How do I access the value of variable a in test1.rb to use further in test1.rb outside SSH?

In test1.rb

Net::SSH.start("host", ava) do |ssh|
   ssh.exec('ruby test2.rb')
end

In host, test2.rb

#!/usr/bin/env ruby

class Value
  def get_value()
    a = 1 + 2
  end
end

v = Value.new
v.get_value()
3
  • 1
    Just write puts v.get_value(). Commented Jan 17, 2014 at 22:11
  • You can set a return code with exit: exit v.get_value() Commented Jan 17, 2014 at 22:16
  • I want to use the value of a in rest of test1.rb Commented Jan 17, 2014 at 22:20

1 Answer 1

3

In your test1.rb script you need to pass a block to the ssh.exec method and in that block you will be able to access anything you puts from test2.rb

Net::SSH.start("127.0.0.1", 'ava', password: '') do |ssh|
  ssh.exec('ruby test2.rb') do |channel, stream, data|
    puts "got: #{data}" if stream == :stdout
  end
end

puts the value you want to pass to test1.rb from test2.rb:

v = Value.new
puts v.get_value()

That will send the value to stdout

More info in the readme: https://github.com/net-ssh/net-ssh#readme

Sign up to request clarification or add additional context in comments.

Comments

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.