【问题标题】:Using SCP/SSH in Ruby with a pem file in an Amazon EC2 instance在 Ruby 中使用 SCP/SSH 和 Amazon EC2 实例中的 pem 文件
【发布时间】:2013-10-06 00:53:01
【问题描述】:

尝试使用 Ruby 脚本从 Amazon EC2(Ubuntu) 实例远程传输或运行命令。 我无法从 ssh 和 scp 的 ruby​​ 文档中弄清楚如何通过 .pem 文件进行身份验证

# download a file from a remote server
Net::SCP.download!("remote.host.com", "username",
"/remote/path", "/local/path",
:password => password)

我也尝试过使用命令行,但这里的问题是主机是动态的,我每次都必须验证“是”

`/usr/bin/scp -i keyfile.pem -r username@some.random.ip:/remote/path /local/path`

The authenticity of host 'some.random.ip (some.random.ip)' can't be established.
ECDSA key fingerprint is some:random:fingerprint.
Are you sure you want to continue connecting (yes/no)? yes

除了不使用命令行代码禁用 SSH 主机密钥检查之外,还有其他方法吗?或者在 net-scp 或 net-ssh gem 中是否有用于 ruby​​ 的选项?

【问题讨论】:

    标签: ruby ubuntu ssh amazon-ec2 scp


    【解决方案1】:

    补充一下,如果你想为Net::SCP.upload! 命令指定一个PEM 文件,例如使用:ssh 哈希键来指定:keys 哈希键值:

    Net::SCP.upload!("remote.host.com", "username",
      "/local/path", "/remote/path", :ssh => { :keys => "/path/to/keyfile.pem" }) do |ch, name, sent, total|
      ...
    end
    

    我从 naveed 的示例和查看 documentation 中发现了这一点。

    【讨论】:

      【解决方案2】:

      我在他们的任何文档中都没有找到的 Ruby 解决方案

      Net::SSH.start( hostname, username, :keys => "/path/to/keyfile.pem" ) do|ssh|
          #process
      end
      

      【讨论】:

      • 谢谢你。我用你的例子来解决Net::SCP.upload!方法。
      【解决方案3】:

      试试这样的命令

      ssh -o StrictHostKeyChecking=no -i keyfile.pem -r username@some.random.ip
      

      您还可以将以下行添加到您的 ~/.ssh/config 文件中,以避免使用 -o 命令行开关

      Host *
          StrictHostKeyChecking no
      

      注意:您可能应该将 * 替换为您访问的远程服务器的域名模式

      同样适用于scp

      【讨论】:

      • 谢谢,但我需要一个不涉及禁用 StrictHostKeyChecking 选项的解决方案(在问题中提到)。无论如何,通过 Ruby 脚本找到了解决方案
      猜你喜欢
      • 2012-07-08
      • 2015-12-30
      • 2020-01-14
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 2020-04-16
      相关资源
      最近更新 更多