Class Net::SSH::Config

  1. lib/net/ssh/config.rb
Parent: Object

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.)

Methods

public class

  1. default_files
  2. for
  3. load
  4. translate

Public class methods

default_files ()

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

[show source]
    # File lib/net/ssh/config.rb, line 41
41:       def default_files
42:         @@default_files
43:       end
for (host, files=default_files)

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.

[show source]
    # File lib/net/ssh/config.rb, line 49
49:       def for(host, files=default_files)
50:         translate(files.inject({}) { |settings, file| load(file, host, settings) })
51:       end
load (path, host, settings={})

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.)

[show source]
     # File lib/net/ssh/config.rb, line 59
 59:       def load(path, host, settings={})
 60:         file = File.expand_path(path)
 61:         return settings unless File.readable?(file)
 62:         
 63:         globals = {}
 64:         matched_host = nil
 65:         multi_host = []
 66:         seen_host = false
 67:         IO.foreach(file) do |line|
 68:           next if line =~ /^\s*(?:#.*)?$/
 69:           
 70:           if line =~ /^\s*(\S+)\s*=(.*)$/
 71:             key, value = $1, $2
 72:           else
 73:             key, value = line.strip.split(/\s+/, 2)
 74:           end
 75: 
 76:           # silently ignore malformed entries
 77:           next if value.nil?
 78: 
 79:           key.downcase!
 80:           value = $1 if value =~ /^"(.*)"$/
 81:           
 82:           value = case value.strip
 83:             when /^\d+$/ then value.to_i
 84:             when /^no$/i then false
 85:             when /^yes$/i then true
 86:             else value
 87:             end
 88:           
 89:           if key == 'host'
 90:             # Support "Host host1 host2 hostN".
 91:             # See http://github.com/net-ssh/net-ssh/issues#issue/6
 92:             multi_host = value.to_s.split(/\s+/)
 93:             matched_host = multi_host.select { |h| host =~ pattern2regex(h) }.first
 94:             seen_host = true
 95:           elsif !seen_host
 96:             if key == 'identityfile'
 97:               (globals[key] ||= []) << value
 98:             else
 99:               globals[key] = value unless settings.key?(key)
100:             end
101:           elsif !matched_host.nil?
102:             if key == 'identityfile'
103:               (settings[key] ||= []) << value
104:             else
105:               settings[key] = value unless settings.key?(key)
106:             end
107:           end
108:         end
109:         
110:         settings = globals.merge(settings) if globals
111:         
112:         return settings
113:       end
translate (settings)

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.

[show source]
     # File lib/net/ssh/config.rb, line 119
119:       def translate(settings)
120:         settings.inject({}) do |hash, (key, value)|
121:           case key
122:           when 'ciphers' then
123:             hash[:encryption] = value.split(/,/)
124:           when 'compression' then
125:             hash[:compression] = value
126:           when 'compressionlevel' then
127:             hash[:compression_level] = value
128:           when 'connecttimeout' then
129:             hash[:timeout] = value
130:           when 'forwardagent' then
131:             hash[:forward_agent] = value
132:           when 'identitiesonly' then
133:             hash[:keys_only] = value
134:           when 'globalknownhostsfile'
135:             hash[:global_known_hosts_file] = value
136:           when 'hostbasedauthentication' then
137:             if value
138:               hash[:auth_methods] ||= []
139:               hash[:auth_methods] << "hostbased"
140:             end
141:           when 'hostkeyalgorithms' then
142:             hash[:host_key] = value.split(/,/)
143:           when 'hostkeyalias' then
144:             hash[:host_key_alias] = value
145:           when 'hostname' then
146:             hash[:host_name] = value
147:           when 'identityfile' then
148:             hash[:keys] = value
149:           when 'macs' then
150:             hash[:hmac] = value.split(/,/)
151:           when 'passwordauthentication'
152:             if value
153:               hash[:auth_methods] ||= []
154:               hash[:auth_methods] << "password"
155:             end
156:           when 'port'
157:             hash[:port] = value
158:           when 'preferredauthentications'
159:             hash[:auth_methods] = value.split(/,/)
160:           when 'proxycommand'
161:             if value and !(value =~ /^none$/)
162:               require 'net/ssh/proxy/command'
163:               hash[:proxy] = Net::SSH::Proxy::Command.new(value)
164:             end
165:           when 'pubkeyauthentication'
166:             if value
167:               hash[:auth_methods] ||= []
168:               hash[:auth_methods] << "publickey"
169:             end
170:           when 'rekeylimit'
171:             hash[:rekey_limit] = interpret_size(value)
172:           when 'user'
173:             hash[:user] = value
174:           when 'userknownhostsfile'
175:             hash[:user_known_hosts_file] = value
176:           end
177:           hash
178:         end
179:       end