[英]Ruby script Net::SSH::HostKeyMismatch but ssh works
我可以ssh到我的aws網絡上的遠程主機,但使用net / ssh在ruby腳本中失敗。 我的gem是Ubuntu 16.04上的net-ssh(4.2.0)。 即使使用non_interactive => false,它也不會提示輸入密碼。
錯誤:
用戶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
keys
數組需要指向您的私鑰。 authorized_keys
是允許登錄當前主機的密鑰的公共指紋。 此外,您似乎已為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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.