Class: SSHKit::Backend::Netssh::KnownHostsKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/sshkit/backends/netssh/known_hosts.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ KnownHostsKeys

Returns a new instance of KnownHostsKeys.



10
11
12
13
14
15
# File 'lib/sshkit/backends/netssh/known_hosts.rb', line 10

def initialize(path)
  super()
  @path = File.expand_path(path)
  @hosts_keys = nil
  @mutex = Mutex.new
end

Instance Method Details

#keys_for(hostlist) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sshkit/backends/netssh/known_hosts.rb', line 17

def keys_for(hostlist)
  keys, hashes = hosts_keys, hosts_hashes
  parse_file unless keys && hashes
  keys, hashes = hosts_keys, hosts_hashes

  host_names = hostlist.split(',')

  keys_found = host_names.map { |h| keys[h] || [] }.compact.inject(:&)
  return keys_found unless keys_found.empty?

  host_names.each do |host|
    hashes.each do |(hmac, salt), hash_keys|
      if OpenSSL::HMAC.digest(sha1, salt, host) == hmac
        return hash_keys
      end
    end
  end

  []
end