Bad knowhow

Authentication failure

I have hosts, that use SSH Host-Based authentication. When I write a script like below (ssh.rb), it throws Net::SSH::AuthenticationFailed.

#!/usr/local/bin/ruby
require 'rubygems'
require 'net/ssh'
Net::SSH.start(
  'target.foo.co.jp',
  'daiba',
  :auth_methods => %w(hostbased),
  :keys => %w(/etc/ssh/ssh_host_dsa_key)
) do |ssh|
  puts ssh.exec!('hostname')
end

Investigation

First, to use "hostbased authentication", your script need to read the local host's private-key. This file is read only by root, so, you must run your script with root privilege.

$ sudo ./ssh.rb

Second, Net::SSH::Authentication::Methods::Hostbased.authenticate_with method

def authenticate_with(identity, next_service, username, key_manager)
  debug { "trying hostbased (#{identity.fingerprint})" }
  client_username = ENV['USER'] || username

checks userneme by "ENV['USER']", then your script gets client_username as 'root'. I don't know why, but my host's /etc/ssh/sshd_configuration deny 'client_username = root' access. This is the problem.

Bad knowhow

I want 'client_username == username', so change the line like below.

def authenticate_with(identity, next_service, username, key_manager)
  debug { "trying hostbased (#{identity.fingerprint})" }
#  client_username = ENV['USER'] || username
  client_username = username

And you can run your script without Net::SSH::AuthenticationFailed.