Wednesday, May 23, 2018

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

Leave a Comment

I can ssh into a remote host on my aws network but using net/ssh fails in a ruby script. my gem is net-ssh(4.2.0) on Ubuntu 16.04. It doesn't prompt for a passphrase even with non_interactive => false.

error:

Authentication failed for user Net::SSH::AuthenticationFailed

Why does this code fail?

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

After editing my code to this I get error

(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 

2 Answers

Answers 1

The keys array needs to point at your private key(s). authorized_keys is the public fingerprints for keys allowed to log in to the current host. Also you seem to have put a private key type in for host_key.

Answers 2

Here is my solution:

#!/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 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment