rubynet-ssh

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


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

Solution

  • 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.