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:
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 39 39: def default_files 40: @@default_files 41: end
Returns an array of locations of OpenSSH configuration files to parse by default.
# 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.
# 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.
# 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.)
# 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: if line =~ /^\s*(\S+)\s*=(.*)$/ 66: key, value = $1, $2 67: else 68: key, value = line.strip.split(/\s+/, 2) 69: end 70: 71: # silently ignore malformed entries 72: next if value.nil? 73: 74: key.downcase! 75: value = $1 if value =~ /^"(.*)"$/ 76: 77: value = case value.strip 78: when /^\d+$/ then value.to_i 79: when /^no$/i then false 80: when /^yes$/i then true 81: else value 82: end 83: 84: if key == 'host' 85: in_match = (host =~ pattern2regex(value)) 86: elsif in_match 87: if key == 'identityfile' 88: settings[key] ||= [] 89: settings[key] << value 90: else 91: settings[key] = value unless settings.key?(key) 92: end 93: end 94: end 95: 96: return settings 97: 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.)
# 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: if line =~ /^\s*(\S+)\s*=(.*)$/ 66: key, value = $1, $2 67: else 68: key, value = line.strip.split(/\s+/, 2) 69: end 70: 71: # silently ignore malformed entries 72: next if value.nil? 73: 74: key.downcase! 75: value = $1 if value =~ /^"(.*)"$/ 76: 77: value = case value.strip 78: when /^\d+$/ then value.to_i 79: when /^no$/i then false 80: when /^yes$/i then true 81: else value 82: end 83: 84: if key == 'host' 85: in_match = (host =~ pattern2regex(value)) 86: elsif in_match 87: if key == 'identityfile' 88: settings[key] ||= [] 89: settings[key] << value 90: else 91: settings[key] = value unless settings.key?(key) 92: end 93: end 94: end 95: 96: return settings 97: 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 103 103: def translate(settings) 104: settings.inject({}) do |hash, (key, value)| 105: case key 106: when 'ciphers' then 107: hash[:encryption] = value.split(/,/) 108: when 'compression' then 109: hash[:compression] = value 110: when 'compressionlevel' then 111: hash[:compression_level] = value 112: when 'connecttimeout' then 113: hash[:timeout] = value 114: when 'forwardagent' then 115: hash[:forward_agent] = value 116: when 'globalknownhostsfile' 117: hash[:global_known_hosts_file] = value 118: when 'hostbasedauthentication' then 119: if value 120: hash[:auth_methods] ||= [] 121: hash[:auth_methods] << "hostbased" 122: end 123: when 'hostkeyalgorithms' then 124: hash[:host_key] = value.split(/,/) 125: when 'hostkeyalias' then 126: hash[:host_key_alias] = value 127: when 'hostname' then 128: hash[:host_name] = value 129: when 'identityfile' then 130: hash[:keys] = value 131: when 'macs' then 132: hash[:hmac] = value.split(/,/) 133: when 'passwordauthentication' 134: if value 135: hash[:auth_methods] ||= [] 136: hash[:auth_methods] << "password" 137: end 138: when 'port' 139: hash[:port] = value 140: when 'preferredauthentications' 141: hash[:auth_methods] = value.split(/,/) 142: when 'pubkeyauthentication' 143: if value 144: hash[:auth_methods] ||= [] 145: hash[:auth_methods] << "publickey" 146: end 147: when 'rekeylimit' 148: hash[:rekey_limit] = interpret_size(value) 149: when 'user' 150: hash[:user] = value 151: when 'userknownhostsfile' 152: hash[:user_known_hosts_file] = value 153: end 154: hash 155: end 156: 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 103 103: def translate(settings) 104: settings.inject({}) do |hash, (key, value)| 105: case key 106: when 'ciphers' then 107: hash[:encryption] = value.split(/,/) 108: when 'compression' then 109: hash[:compression] = value 110: when 'compressionlevel' then 111: hash[:compression_level] = value 112: when 'connecttimeout' then 113: hash[:timeout] = value 114: when 'forwardagent' then 115: hash[:forward_agent] = value 116: when 'globalknownhostsfile' 117: hash[:global_known_hosts_file] = value 118: when 'hostbasedauthentication' then 119: if value 120: hash[:auth_methods] ||= [] 121: hash[:auth_methods] << "hostbased" 122: end 123: when 'hostkeyalgorithms' then 124: hash[:host_key] = value.split(/,/) 125: when 'hostkeyalias' then 126: hash[:host_key_alias] = value 127: when 'hostname' then 128: hash[:host_name] = value 129: when 'identityfile' then 130: hash[:keys] = value 131: when 'macs' then 132: hash[:hmac] = value.split(/,/) 133: when 'passwordauthentication' 134: if value 135: hash[:auth_methods] ||= [] 136: hash[:auth_methods] << "password" 137: end 138: when 'port' 139: hash[:port] = value 140: when 'preferredauthentications' 141: hash[:auth_methods] = value.split(/,/) 142: when 'pubkeyauthentication' 143: if value 144: hash[:auth_methods] ||= [] 145: hash[:auth_methods] << "publickey" 146: end 147: when 'rekeylimit' 148: hash[:rekey_limit] = interpret_size(value) 149: when 'user' 150: hash[:user] = value 151: when 'userknownhostsfile' 152: hash[:user_known_hosts_file] = value 153: end 154: hash 155: end 156: end