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.)
Returns an array of locations of OpenSSH configuration files to parse by default.
# File lib/net/ssh/config.rb, line 41 41: def default_files 42: @@default_files 43: end
Loads the configuration data for the given host from all of the given files (defaulting to the list of files returned by #), translates the resulting hash into the options recognized by Net::SSH, and returns them.
# 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 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 # for how to convert the OpenSSH options into Net::SSH options.)
# 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$/ then false 85: when /^yes$/ 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
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 119 119: def translate(settings) 120: settings.inject({}) do |hash, (key, value)| 121: case key 122: when 'bindaddress' then 123: hash[:bind_address] = value 124: when 'ciphers' then 125: hash[:encryption] = value.split(/,/) 126: when 'compression' then 127: hash[:compression] = value 128: when 'compressionlevel' then 129: hash[:compression_level] = value 130: when 'connecttimeout' then 131: hash[:timeout] = value 132: when 'forwardagent' then 133: hash[:forward_agent] = value 134: when 'identitiesonly' then 135: hash[:keys_only] = value 136: when 'globalknownhostsfile' 137: hash[:global_known_hosts_file] = value 138: when 'hostbasedauthentication' then 139: if value 140: hash[:auth_methods] ||= [] 141: hash[:auth_methods] << "hostbased" 142: end 143: when 'hostkeyalgorithms' then 144: hash[:host_key] = value.split(/,/) 145: when 'hostkeyalias' then 146: hash[:host_key_alias] = value 147: when 'hostname' then 148: hash[:host_name] = value 149: when 'identityfile' then 150: hash[:keys] = value 151: when 'macs' then 152: hash[:hmac] = value.split(/,/) 153: when 'passwordauthentication' 154: if value 155: hash[:auth_methods] ||= [] 156: hash[:auth_methods] << "password" 157: end 158: when 'port' 159: hash[:port] = value 160: when 'preferredauthentications' 161: hash[:auth_methods] = value.split(/,/) 162: when 'proxycommand' 163: if value and !(value =~ /^none$/) 164: require 'net/ssh/proxy/command' 165: hash[:proxy] = Net::SSH::Proxy::Command.new(value) 166: end 167: when 'pubkeyauthentication' 168: if value 169: hash[:auth_methods] ||= [] 170: hash[:auth_methods] << "publickey" 171: end 172: when 'rekeylimit' 173: hash[:rekey_limit] = interpret_size(value) 174: when 'user' 175: hash[:user] = value 176: when 'userknownhostsfile' 177: hash[:user_known_hosts_file] = value 178: end 179: hash 180: end 181: end
Converts the given size into an integer number of bytes.
# File lib/net/ssh/config.rb, line 196 196: def interpret_size(size) 197: case size 198: when /k$/ then size.to_i * 1024 199: when /m$/ then size.to_i * 1024 * 1024 200: when /g$/ then size.to_i * 1024 * 1024 * 1024 201: else size.to_i 202: end 203: end
Converts an ssh_config pattern into a regex for matching against host names.
# File lib/net/ssh/config.rb, line 187 187: def pattern2regex(pattern) 188: pattern = "^" + pattern.to_s.gsub(/\./, "\\."). 189: gsub(/\?/, '.'). 190: gsub(/([+\/])/, '\\\0'). 191: gsub(/\*/, '.*') + "$" 192: Regexp.new(pattern, true) 193: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.