4

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 => "[email protected]"
 ) do |session|
  output = session.exec!('ls')
  puts output
 end
2
  • What is your question? Commented May 15, 2018 at 5:59
  • The error shows my question. Commented May 15, 2018 at 13:10

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ssh-rsa isn't a host key, it's a key type. Remove that.
1

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 => "[email protected]"
 ) 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.