0

I have a Rails Application from which I will call one Ruby Definition in the controller, like this.

ssh root@host "uptime" >> /tmp/output

When I doing this, only the /tmp/output is created but not the content. When I running the same from simple Ruby script its working fine.

my controller definition

  def chefclient1
    `ssh root@host "uptime" >> /tmp/output`
    @time = Time.now
  end

my view

= link_to('Start uptime', host_chefclient1_path)

1 Answer 1

1

You can use net-ssh gem to access remote host via ssh fo a ruby app. Set your environment up to:

HOSTNAME = 'host'
USER = 'root'
PASS = 'password'

Add to your_helper.rb: something like:

def chefclient1
   result = nil
   Net::SSH.start( ENV[ 'HOSTNAME' ], ENV[ 'USER' ], :password => ENV[ 'PASS' ] ) do| ssh |
      result = ssh.exec! 'uptime'
      puts result
   end

   File.open( '/tmp/output', 'w' ) {| f | f.puts result }
end

And use the helper method as you with from a view.

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.