Class Net::SSH::Config
In: lib/net/ssh/config.rb
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
  • Macs => maps to the :hmac option
  • PasswordAuthentication => maps to the :auth_methods option
  • Port => :port
  • PreferredAuthentications => maps to the :auth_methods 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

default_files   default_files   for   for   load   load   translate   translate  

Public Class methods

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

[Source]

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

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

[Source]

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

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.

[Source]

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

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.

[Source]

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

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

[Source]

    # File lib/net/ssh/config.rb, line 57
57:       def load(file, host, settings={})
58:         file = File.expand_path(file)
59:         return settings unless File.readable?(file)
60: 
61:         in_match = false
62:         IO.foreach(file) do |line|
63:           next if line =~ /^\s*(?:#.*)?$/
64: 
65:           key, value = line.strip.split(/\s+/, 2)
66:           key.downcase!
67: 
68:           value = $1 if value =~ /^"(.*)"$/
69:           value = case value.strip
70:             when /^\d+$/ then value.to_i
71:             when /^no$/i then false
72:             when /^yes$/i then true
73:             else value
74:             end
75: 
76:           if key == 'host'
77:             in_match = (host =~ pattern2regex(value))
78:           elsif in_match
79:             if key == 'identityfile'
80:               settings[key] ||= []
81:               settings[key] << value
82:             else
83:               settings[key] = value unless settings.key?(key)
84:             end
85:           end
86:         end
87: 
88:         return settings
89:       end

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

[Source]

    # File lib/net/ssh/config.rb, line 57
57:       def load(file, host, settings={})
58:         file = File.expand_path(file)
59:         return settings unless File.readable?(file)
60: 
61:         in_match = false
62:         IO.foreach(file) do |line|
63:           next if line =~ /^\s*(?:#.*)?$/
64: 
65:           key, value = line.strip.split(/\s+/, 2)
66:           key.downcase!
67: 
68:           value = $1 if value =~ /^"(.*)"$/
69:           value = case value.strip
70:             when /^\d+$/ then value.to_i
71:             when /^no$/i then false
72:             when /^yes$/i then true
73:             else value
74:             end
75: 
76:           if key == 'host'
77:             in_match = (host =~ pattern2regex(value))
78:           elsif in_match
79:             if key == 'identityfile'
80:               settings[key] ||= []
81:               settings[key] << value
82:             else
83:               settings[key] = value unless settings.key?(key)
84:             end
85:           end
86:         end
87: 
88:         return settings
89:       end

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.

[Source]

     # File lib/net/ssh/config.rb, line 95
 95:       def translate(settings)
 96:         settings.inject({}) do |hash, (key, value)|
 97:           case key
 98:           when 'ciphers' then
 99:             hash[:encryption] = value.split(/,/)
100:           when 'compression' then
101:             hash[:compression] = value
102:           when 'compressionlevel' then
103:             hash[:compression_level] = value
104:           when 'connecttimeout' then
105:             hash[:timeout] = value
106:           when 'forwardagent' then
107:             hash[:forward_agent] = value
108:           when 'globalknownhostsfile'
109:             hash[:global_known_hosts_file] = value
110:           when 'hostbasedauthentication' then
111:             if value
112:               hash[:auth_methods] ||= []
113:               hash[:auth_methods] << "hostbased"
114:             end
115:           when 'hostkeyalgorithms' then
116:             hash[:host_key] = value.split(/,/)
117:           when 'hostkeyalias' then
118:             hash[:host_key_alias] = value
119:           when 'hostname' then
120:             hash[:host_name] = value
121:           when 'identityfile' then
122:             hash[:keys] = value
123:           when 'macs' then
124:             hash[:hmac] = value.split(/,/)
125:           when 'passwordauthentication'
126:             if value
127:               hash[:auth_methods] ||= []
128:               hash[:auth_methods] << "password"
129:             end
130:           when 'port'
131:             hash[:port] = value
132:           when 'preferredauthentications'
133:             hash[:auth_methods] = value.split(/,/)
134:           when 'pubkeyauthentication'
135:             if value
136:               hash[:auth_methods] ||= []
137:               hash[:auth_methods] << "publickey"
138:             end
139:           when 'rekeylimit'
140:             hash[:rekey_limit] = interpret_size(value)
141:           when 'user'
142:             hash[:user] = value
143:           when 'userknownhostsfile'
144:             hash[:user_known_hosts_file] = value
145:           end
146:           hash
147:         end
148:       end

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.

[Source]

     # File lib/net/ssh/config.rb, line 95
 95:       def translate(settings)
 96:         settings.inject({}) do |hash, (key, value)|
 97:           case key
 98:           when 'ciphers' then
 99:             hash[:encryption] = value.split(/,/)
100:           when 'compression' then
101:             hash[:compression] = value
102:           when 'compressionlevel' then
103:             hash[:compression_level] = value
104:           when 'connecttimeout' then
105:             hash[:timeout] = value
106:           when 'forwardagent' then
107:             hash[:forward_agent] = value
108:           when 'globalknownhostsfile'
109:             hash[:global_known_hosts_file] = value
110:           when 'hostbasedauthentication' then
111:             if value
112:               hash[:auth_methods] ||= []
113:               hash[:auth_methods] << "hostbased"
114:             end
115:           when 'hostkeyalgorithms' then
116:             hash[:host_key] = value.split(/,/)
117:           when 'hostkeyalias' then
118:             hash[:host_key_alias] = value
119:           when 'hostname' then
120:             hash[:host_name] = value
121:           when 'identityfile' then
122:             hash[:keys] = value
123:           when 'macs' then
124:             hash[:hmac] = value.split(/,/)
125:           when 'passwordauthentication'
126:             if value
127:               hash[:auth_methods] ||= []
128:               hash[:auth_methods] << "password"
129:             end
130:           when 'port'
131:             hash[:port] = value
132:           when 'preferredauthentications'
133:             hash[:auth_methods] = value.split(/,/)
134:           when 'pubkeyauthentication'
135:             if value
136:               hash[:auth_methods] ||= []
137:               hash[:auth_methods] << "publickey"
138:             end
139:           when 'rekeylimit'
140:             hash[:rekey_limit] = interpret_size(value)
141:           when 'user'
142:             hash[:user] = value
143:           when 'userknownhostsfile'
144:             hash[:user_known_hosts_file] = value
145:           end
146:           hash
147:         end
148:       end

[Validate]