Class: Net::SSH::KnownHosts

### Home Classes Methods

### In Files

- lib/net/ssh/known_hosts.rb

### Parent

Object

### Methods

- ::add
- ::hostfiles
- ::new
- ::search_for
- ::search_in
- #add
- #keys_for

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Net
- Net::SSH
- Net::SSH::Authentication
- Net::SSH::Authentication::Agent
- Net::SSH::Authentication::Agent::Comment
- Net::SSH::Authentication::AgentError
- Net::SSH::Authentication::AgentNotAvailable
- Net::SSH::Authentication::Constants
- Net::SSH::Authentication::DisallowedMethod
- Net::SSH::Authentication::KeyManager
- Net::SSH::Authentication::KeyManagerError
- Net::SSH::Authentication::Methods
- Net::SSH::Authentication::Methods::Abstract
- Net::SSH::Authentication::Methods::Hostbased
- Net::SSH::Authentication::Methods::KeyboardInteractive
- Net::SSH::Authentication::Methods::None
- Net::SSH::Authentication::Methods::Password
- Net::SSH::Authentication::Methods::Publickey
- Net::SSH::Authentication::Session
- Net::SSH::AuthenticationFailed
- Net::SSH::Buffer
- Net::SSH::ChannelOpenFailed
- Net::SSH::ChannelRequestFailed
- Net::SSH::Compat
- Net::SSH::Config
- Net::SSH::Connection
- Net::SSH::Connection::Channel
- Net::SSH::Connection::Constants
- Net::SSH::Connection::Session
- Net::SSH::Connection::Session::NilChannel
- Net::SSH::Connection::Term
- Net::SSH::ConnectionTimeout
- Net::SSH::Disconnect
- Net::SSH::Exception
- Net::SSH::HostKeyError
- Net::SSH::HostKeyMismatch
- Net::SSH::HostKeyUnknown
- Net::SSH::KeyFactory
- Net::SSH::KnownHosts
- Net::SSH::Loggable
- Net::SSH::Packet
- Net::SSH::PromptMethods
- Net::SSH::PromptMethods::Clear
- Net::SSH::PromptMethods::Highline
- Net::SSH::PromptMethods::Termios
- Net::SSH::Proxy
- Net::SSH::Proxy::Command
- Net::SSH::Proxy::ConnectError
- Net::SSH::Proxy::Error
- Net::SSH::Proxy::HTTP
- Net::SSH::Proxy::SOCKS4
- Net::SSH::Proxy::SOCKS5
- Net::SSH::Proxy::UnauthorizedError
- Net::SSH::Test
- Net::SSH::Test::Channel
- Net::SSH::Test::Extensions
- Net::SSH::Test::Extensions::BufferedIo
- Net::SSH::Test::Extensions::Channel
- Net::SSH::Test::Extensions::IO
- Net::SSH::Test::Extensions::IO::ClassMethods
- Net::SSH::Test::Extensions::PacketStream
- Net::SSH::Test::Kex
- Net::SSH::Test::LocalPacket
- Net::SSH::Test::Packet
- Net::SSH::Test::RemotePacket
- Net::SSH::Test::Script
- Net::SSH::Test::Socket
- Net::SSH::Transport
- Net::SSH::Transport::Algorithms
- Net::SSH::Transport::CTR
- Net::SSH::Transport::CipherFactory
- Net::SSH::Transport::Constants
- Net::SSH::Transport::HMAC
- Net::SSH::Transport::HMAC::Abstract
- Net::SSH::Transport::HMAC::MD5
- Net::SSH::Transport::HMAC::MD5_96
- Net::SSH::Transport::HMAC::None
- Net::SSH::Transport::HMAC::RIPEMD160
- Net::SSH::Transport::HMAC::SHA1
- Net::SSH::Transport::HMAC::SHA1_96
- Net::SSH::Transport::HMAC::SHA2_256
- Net::SSH::Transport::HMAC::SHA2_256_96
- Net::SSH::Transport::HMAC::SHA2_512
- Net::SSH::Transport::HMAC::SHA2_512_96
- Net::SSH::Transport::IdentityCipher
- Net::SSH::Transport::Kex
- Net::SSH::Transport::Kex::DiffieHellmanGroup14SHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroup1SHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroupExchangeSHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroupExchangeSHA256
- Net::SSH::Transport::Kex::EcdhSHA2NistP256
- Net::SSH::Transport::Kex::EcdhSHA2NistP384
- Net::SSH::Transport::Kex::EcdhSHA2NistP521
- Net::SSH::Transport::KeyExpander
- Net::SSH::Transport::PacketStream
- Net::SSH::Transport::ServerVersion
- Net::SSH::Transport::Session
- Net::SSH::Transport::State
- Net::SSH::Verifiers
- Net::SSH::Verifiers::Lenient
- Net::SSH::Verifiers::Null
- Net::SSH::Verifiers::Secure
- Net::SSH::Verifiers::Strict
- Net::SSH::Version
- OpenSSL
- OpenSSL::BN
- OpenSSL::PKey
- OpenSSL::PKey::DH
- OpenSSL::PKey::DSA
- OpenSSL::PKey::EC
- OpenSSL::PKey::OpenSSL
- OpenSSL::PKey::OpenSSL::PKey
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL::PKey
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL::PKey::ECError
- OpenSSL::PKey::PKey
- OpenSSL::PKey::RSA
- String

No matching classes.

# Net::SSH::KnownHosts

Searches an OpenSSH-style known-host file for a given host, and returns all matching keys. This is used to implement host-key verification, as well as to determine what key a user prefers to use for a given host.

This is used internally by Net::SSH, and will never need to be used directly by consumers of the library.

### Constants

SUPPORTED_TYPE

### Attributes

source[R]

The host-key file name that this KnownHosts instance will use to search for keys.

### Public Class Methods

add(host, key, options={}) click to toggle source

Looks in all user known host files (see KnownHosts.hostfiles) and tries to add an entry for the given host and key to the first file it is able to.

    # File lib/net/ssh/known_hosts.rb, line 64
    def add(host, key, options={})
      hostfiles(options, :user).each do |file|
        begin
          KnownHosts.new(file).add(host, key)
          return
        rescue SystemCallError
          # try the next hostfile
        end
      end
    end

hostfiles(options, which=:all) click to toggle source

Looks in the given `options` hash for the :user_known_hosts_file and :global_known_hosts_file keys, and returns an array of all known hosts files. If the :user_known_hosts_file key is not set, the default is returned (~/.ssh/known_hosts and ~/.ssh/known_hosts2). If :global_known_hosts_file is not set, the default is used (/etc/ssh/known_hosts and /etc/ssh/known_hosts2).

If you only want the user known host files, you can pass :user as the second option.

    # File lib/net/ssh/known_hosts.rb, line 47
    def hostfiles(options, which=:all)
      files = []

      if which == :all || which == :user
        files += Array(options[:user_known_hosts_file] || %(~/.ssh/known_hosts ~/.ssh/known_hosts2))
      end

      if which == :all || which == :global
        files += Array(options[:global_known_hosts_file] || %(/etc/ssh/known_hosts /etc/ssh/known_hosts2))
      end

      return files
    end

new(source) click to toggle source

Instantiate a new KnownHosts instance that will search the given known-hosts file. The path is expanded file File.expand_path.

    # File lib/net/ssh/known_hosts.rb, line 82
    def initialize(source)
      @source = File.expand_path(source)
    end

search_for(host, options={}) click to toggle source

Searches all known host files (see KnownHosts.hostfiles) for all keys of the given host. Returns an array of keys found.

    # File lib/net/ssh/known_hosts.rb, line 28
    def search_for(host, options={})
      search_in(hostfiles(options), host)
    end

search_in(files, host) click to toggle source

Search for all known keys for the given host, in every file given in the `files` array. Returns the list of keys.

    # File lib/net/ssh/known_hosts.rb, line 34
    def search_in(files, host)
      files.map { |file| KnownHosts.new(file).keys_for(host) }.flatten
    end

### Public Instance Methods

add(host, key) click to toggle source

Tries to append an entry to the current source file for the given host and key. If it is unable to (because the file is not writable, for instance), an exception will be raised.

    # File lib/net/ssh/known_hosts.rb, line 133
    def add(host, key)
      File.open(source, "a") do |file|
        blob = [Net::SSH::Buffer.from(:key, key).to_s].pack("m*").gsub(/\s/, "")
        file.puts "#{host} #{key.ssh_type} #{blob}"
      end
    end

keys_for(host) click to toggle source

Returns an array of all keys that are known to be associatd with the given host. The `host` parameter is either the domain name or ip address of the host, or both (comma-separated). Additionally, if a non-standard port is being used, it may be specified by putting the host (or ip, or both) in square brackets, and appending the port outside the brackets after a colon. Possible formats for `host`, then, are;

    "net.ssh.test"
    "1.2.3.4"
    "net.ssh.test,1.2.3.4"
    "[net.ssh.test]:5555"
    "[1,2,3,4]:5555"
    "[net.ssh.test]:5555,[1.2.3.4]:5555

    # File lib/net/ssh/known_hosts.rb, line 99
    def keys_for(host)
      keys = []
      return keys unless File.readable?(source)

      entries = host.split(/,/)

      File.open(source) do |file|
        scanner = StringScanner.new("")
        file.each_line do |line|
          scanner.string = line

          scanner.skip(/\s*/)
          next if scanner.match?(/$|#/)

          hostlist = scanner.scan(/\S+/).split(/,/)
          next unless entries.all? { |entry| hostlist.include?(entry) }

          scanner.skip(/\s*/)
          type = scanner.scan(/\S+/)

          next unless SUPPORTED_TYPE.include?(type)

          scanner.skip(/\s*/)
          blob = scanner.rest.unpack("m*").first
          keys << Net::SSH::Buffer.new(blob).read_key
        end
      end

      keys
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.