ruby-on-rails - Net::SSH 从生产 rails 控制台工作,从生产 webapp 工作 AuthenticationFailed

标签 ruby-on-rails ruby ruby-on-rails-3 delayed-job net-ssh

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

奇怪的是,Net::SSH 在生产环境中的控制台上工作得很好,但是当我在生产环境中提交表单时,它因 AuthenticationFailed 而失败。控制台和 webapp 在开发中都运行良好。

错误:

Net::SSH::AuthenticationFailed (my_ssh_username):

app/models/branch.rb:69:in `ssh_to_machine'

app/controllers/branches_controller.rb:55:in `update'

Controller 的更新 Action :

  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=production)并且从不从 webapp 工作。

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

最佳答案

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

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

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

关于ruby-on-rails - Net::SSH 从生产 rails 控制台工作,从生产 webapp 工作 AuthenticationFailed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7533170/

相关文章:

ruby-on-rails - 如何解决错误 "bundler could not find compatible version for gem X"

c# - 这个逻辑运算符有捷径吗?

ruby-on-rails - not_nil 有 Ruby 或 Ruby 主义吗?与零相反?方法?

ruby-on-rails - 笛卡尔积 ruby

ruby-on-rails - 修改 en.yml 文件以验证错误消息 rails 3

ruby-on-rails - crossdomain.xml 中的 secure false 是做什么用的?

ruby-on-rails - 包含组和计数的 Rails 查询必须包含零

javascript - 无法使用 Rails 3.0 的 best-in-place gem 编辑记录

ruby - 如何在 Nokogiri/Ruby 解析期间检查页面上是否存在元素?

ruby-on-rails - Omniauth session 在浏览器关闭时过期