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

时间:2018-05-14 19:52:11

标签: ruby net-ssh

我可以ssh到我的aws网络上的远程主机,但在ruby脚本中使用net / ssh失败。我的gem是Ubuntu 16.04上的net-ssh(4.2.0)。即使使用non_interactive =>它也不会提示输入密码。假的。

错误:

  

用户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

将代码编辑到此后,我收到错误

  

(净:: 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

2 个答案:

答案 0 :(得分:4)

keys数组需要指向您的私钥。 authorized_keys是允许登录当前主机的密钥的公共指纹。此外,您似乎已为host_key添加了私钥类型。

答案 1 :(得分:1)

这是我的解决方案:

#!/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