Net :: SSH适用于生产rails控制台,AuthenticationFailed来自生产webapp

我有一个rails应用程序,用户可以在其中提交表单,然后通过ssh连接到远程服务器以调用脚本。 最终我打算使用delayed_job或类似的东西,但即使是一个简单的测试我也无法让它在生产中工作。

奇怪的是,Net :: SSH在生产环境中从控制台运行得很好,但是当我在生产环境中提交表单时,它失败了。 控制台和webapp都可以在开发中正常运行。

错误:

Net :: SSH :: AuthenticationFailed(my_ssh_username):

app / models / branch.rb:69:在`ssh_to_machine’中

app / controllers / branches_controller.rb:55:在`update’中

控制器的更新动作:

def update @branch = Branch.find(params[:id]) if @branch.update_attributes(params[:branch]) @branch.ssh_to_machine(@branch.hostname, @branch.user_name, @branch.command_to_run) redirect_to @branch, :notice => "Update request now processing." else render :action => 'edit' end end 

方法我正在调用,主要是从Net :: SSH api示例中复制/粘贴:

 def ssh_to_machine(host_name, user_name, command_to_run) require 'net/ssh' Net::SSH.start(host_name, user_name, { :verbose => Logger::DEBUG, :keys => %w{ /home/www-data/.ssh/my_ssh_username_id_rsa }, :auth_methods => %w{ publickey } }) do |ssh| # capture all stderr and stdout output from a remote process output = ssh.exec!("hostname") # run multiple processes in parallel to completion ssh.exec command_to_run ssh.loop end end 

我有没有尝试过:verbose,:keys,:auth_methods; 每次都要小心重启apache,但是在生产中它始终可以在控制台上运行(在调用’rails c’之前RAILS_ENV =生成导出)并且从不在webapp中运行。

当我从webapp调用它时,我也欢迎任何有关如何获得增强日志记录的建议 – :verbose在控制台上为我工作但没有向我的production.log添加任何内容。

当您从控制台运行它时,您正在使用自己的帐户,对吧?

这有点奇怪,但我的猜测是你的生产网络应用程序在一个没有“/home/www-data/.ssh/my_ssh_username_id_rsa”读取权限的帐户下运行。

根据您的描述,它几乎必须是某种权限问题。