Class: Net::SSH::Config

### Home Classes Methods

### In Files

- lib/net/ssh/config.rb

### Parent

Object

### Methods

- ::default_files
- ::for
- ::load
- ::translate

### 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::Config

The Net::SSH::Config class is used to parse OpenSSH configuration files, and translates that syntax into the configuration syntax that Net::SSH understands. This lets Net::SSH scripts read their configuration (to some extent) from OpenSSH configuration files (~/.ssh/config, /etc/ssh_config, and so forth).

Only a subset of OpenSSH configuration options are understood:

- Ciphers => maps to the :encryption option

- Compression => :compression

- CompressionLevel => :compression_level

- ConnectTimeout => maps to the :timeout option

- ForwardAgent => :forward_agent

- GlobalKnownHostsFile => :global_known_hosts_file

- HostBasedAuthentication => maps to the :auth_methods option

- HostKeyAlgorithms => maps to :host_key option

- HostKeyAlias => :host_key_alias

- HostName => :host_name

- IdentityFile => maps to the :keys option

- IdentitiesOnly => :keys_only

- Macs => maps to the :hmac option

- PasswordAuthentication => maps to the :auth_methods option

- Port => :port

- PreferredAuthentications => maps to the :auth_methods option

- ProxyCommand => maps to the :proxy option

- RekeyLimit => :rekey_limit

- User => :user

- UserKnownHostsFile => :user_known_hosts_file

Note that you will never need to use this class directly--you can control whether the OpenSSH configuration files are read by passing the :config option to Net::SSH.start. (They are, by default.)

### Public Class Methods

default_files() click to toggle source

Returns an array of locations of OpenSSH configuration files to parse by default.

    # File lib/net/ssh/config.rb, line 41
    def default_files
      @@default_files
    end

for(host, files=default_files) click to toggle source

Loads the configuration data for the given `host` from all of the given `files` (defaulting to the list of files returned by default_files), translates the resulting hash into the options recognized by Net::SSH, and returns them.

    # File lib/net/ssh/config.rb, line 49
    def for(host, files=default_files)
      translate(files.inject({}) { |settings, file| load(file, host, settings) })
    end

load(path, host, settings={}) click to toggle source

Load the OpenSSH configuration settings in the given `file` for the given `host`. If `settings` is given, the options are merged into that hash, with existing values taking precedence over newly parsed ones. Returns a hash containing the OpenSSH options. (See translate for how to convert the OpenSSH options into Net::SSH options.)

    # File lib/net/ssh/config.rb, line 59
    def load(path, host, settings={})
      file = File.expand_path(path)
      return settings unless File.readable?(file)

      globals = {}
      matched_host = nil
      multi_host = []
      seen_host = false
      IO.foreach(file) do |line|
        next if line =~ /^\s*(?:#.*)?$/

        if line =~ /^\s*(\S+)\s*=(.*)$/
          key, value = $1, $2
        else
          key, value = line.strip.split(/\s+/, 2)
        end

        # silently ignore malformed entries
        next if value.nil?

        key.downcase!
        value = $1 if value =~ /^"(.*)"$/

        value = case value.strip
          when /^\d+$/ then value.to_i
          when /^no$/ then false
          when /^yes$/ then true
          else value
          end

        if key == 'host'
          # Support "Host host1 host2 hostN".
          # See http://github.com/net-ssh/net-ssh/issues#issue/6
          multi_host = value.to_s.split(/\s+/)
          matched_host = multi_host.select { |h| host =~ pattern2regex(h) }.first
          seen_host = true
          settings[key] = host
        elsif !seen_host
          if key == 'identityfile'
            (globals[key] ||= []) << value
          else
            globals[key] = value unless settings.key?(key)
          end
        elsif !matched_host.nil?
          if key == 'identityfile'
            (settings[key] ||= []) << value
          else
            settings[key] = value unless settings.key?(key)
          end
        end
      end

      settings = globals.merge(settings) if globals

      return settings
    end

translate(settings) click to toggle source

Given a hash of OpenSSH configuration options, converts them into a hash of Net::SSH options. Unrecognized options are ignored. The `settings` hash must have Strings for keys, all downcased, and the returned hash will have Symbols for keys.

    # File lib/net/ssh/config.rb, line 120
    def translate(settings)
      settings.inject({}) do |hash, (key, value)|
        case key
        when 'bindaddress' then
          hash[:bind_address] = value
        when 'ciphers' then
          hash[:encryption] = value.split(/,/)
        when 'compression' then
          hash[:compression] = value
        when 'compressionlevel' then
          hash[:compression_level] = value
        when 'connecttimeout' then
          hash[:timeout] = value
        when 'forwardagent' then
          hash[:forward_agent] = value
        when 'identitiesonly' then
          hash[:keys_only] = value
        when 'globalknownhostsfile'
          hash[:global_known_hosts_file] = value
        when 'hostbasedauthentication' then
          if value
            hash[:auth_methods] ||= []
            hash[:auth_methods] << "hostbased"
          end
        when 'hostkeyalgorithms' then
          hash[:host_key] = value.split(/,/)
        when 'hostkeyalias' then
          hash[:host_key_alias] = value
        when 'hostname' then
          hash[:host_name] = value.gsub(/%h/, settings['host'])
        when 'identityfile' then
          hash[:keys] = value
        when 'macs' then
          hash[:hmac] = value.split(/,/)
        when 'passwordauthentication'
          if value
            hash[:auth_methods] ||= []
            hash[:auth_methods] << "password"
          end
        when 'port'
          hash[:port] = value
        when 'preferredauthentications'
          hash[:auth_methods] = value.split(/,/)
        when 'proxycommand'
          if value and !(value =~ /^none$/)
            require 'net/ssh/proxy/command'
            hash[:proxy] = Net::SSH::Proxy::Command.new(value)
          end
        when 'pubkeyauthentication'
          if value
            hash[:auth_methods] ||= []
            hash[:auth_methods] << "publickey"
          end
        when 'rekeylimit'
          hash[:rekey_limit] = interpret_size(value)
        when 'user'
          hash[:user] = value
        when 'userknownhostsfile'
          hash[:user_known_hosts_file] = value
        when 'sendenv'
          multi_send_env = value.to_s.split(/\s+/)
          hash[:send_env] = multi_send_env.map { |e| Regexp.new pattern2regex(e).source, false }
        end
        hash
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.