Ruby 脚本 Net::SSH::HostKeyMismatch 但 ssh 有效

Ruby script Net::SSH::HostKeyMismatch but ssh works

我可以通过 ssh 连接到我的 aws 网络上的远程主机,但在 ruby 脚本中使用 net/ssh 失败。我的 gem 是 Ubuntu 16.04 上的 net-ssh(4.2.0)。即使 non_interactive => false.

也不会提示输入密码

错误:

Authentication failed for user Net::SSH::AuthenticationFailed

为什么这段代码会失败?

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = 'myhost'

Net::SSH.start(HOST,
:auth_methods => ['publickey'],
:passphrase => 'mypassphrase',
:non_interactive => true,
:host_key => "ssh-rsa",
:keys => [ '/home/markhorrocks/.ssh/id_rsa' ]
 ) do |session|
  output = session.exec!('ls')
  puts output
 end

将我的代码编辑成这个后出现错误

(Net::SSH::HostKeyMismatch)

HOST = 'myhost'
USER = 'markhorrocks'

Net::SSH.start(HOST, USER,
:auth_methods => ['publickey'],
:passphrase => 'mypassphrase',
:non_interactive => true,
:host_key => "ssh-rsa",
  :encryption => "blowfish-cbc",
:keys => [ '/home/markhorrocks/.ssh/id_rsa' ],
:port => '1234',
:forward_agent => true,
  :compression => "zlib@openssh.com"
 ) do |session|
  output = session.exec!('ls')
  puts output
 end

keys 数组需要指向您的私钥。 authorized_keys是允许登录当前主机的密钥的public指纹。您似乎还为 host_key.

输入了私钥类型

这是我的解决方案:

#!/usr/bin/env ruby

require 'net/ssh'

HOST = 'myhost'
USER = 'markhorrocks'

Net::SSH.start(HOST, USER,
:auth_methods => ['publickey'],
:passphrase => 'mypassphrase',
:non_interactive => true,
:host_key => "ssh-rsa",
:encryption => "blowfish-cbc",
:keys => [ '/home/markhorrocks/.ssh/id_rsa' ],
:port => '1234',
:forward_agent => true,
:verify_host_key => false,
:compression => "zlib@openssh.com"
 ) do |session|
       begin
          rescue Net::SSH::HostKeyMismatch => e
          puts "remembering new key: #{e.fingerprint}"
          e.remember_host!
          retry
       end

  output = session.exec!('ls')
  puts output
 end