Ruby on rails 使用来自Rails应用程序的SSH远程执行命令
我有一个Rails应用程序,我将从中调用控制器中的一个Ruby定义,如下所示Ruby on rails 使用来自Rails应用程序的SSH远程执行命令,ruby-on-rails,ruby,ssh,remote-access,Ruby On Rails,Ruby,Ssh,Remote Access,我有一个Rails应用程序,我将从中调用控制器中的一个Ruby定义,如下所示 ssh root@host "uptime" >> /tmp/output 执行此操作时,只创建/tmp/output,而不创建内容。 当我从简单的Ruby脚本运行相同的代码时,它工作得很好 我的控制器定义 def chefclient1 `ssh root@host "uptime" >> /tmp/output` @time = Time.now end 我的看法
ssh root@host "uptime" >> /tmp/output
执行此操作时,只创建/tmp/output,而不创建内容。
当我从简单的Ruby脚本运行相同的代码时,它工作得很好
我的控制器定义
def chefclient1
`ssh root@host "uptime" >> /tmp/output`
@time = Time.now
end
我的看法
= link_to('Start uptime', host_chefclient1_path)
您可以使用netssh
gem通过ssh访问远程主机,以获得ruby应用程序。将您的环境设置为:
HOSTNAME = 'host'
USER = 'root'
PASS = 'password'
添加到您的\u助手。rb:类似于:
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
并在视图中使用辅助对象方法