From 17cadf135c3f16b5ddf2ddc59ac4fbda98df3ce1 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Mon, 2 Feb 2009 22:40:54 -0700 Subject: make it easier to query SSH configuration in a standard way --- CHANGELOG.rdoc | 5 +++++ lib/net/ssh.rb | 27 ++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 0f9b323..7076fa4 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,8 @@ +=== (unreleased) + +* Added Net::SSH.configuration_for to make it easier to query the SSH configuration file(s) [Jamis Buck] + + === 2.0.9 / 1 Feb 2009 * Specifying non-nil user argument overrides user in .ssh/config [Jamis Buck] diff --git a/lib/net/ssh.rb b/lib/net/ssh.rb index a752392..0d36d78 100644 --- a/lib/net/ssh.rb +++ b/lib/net/ssh.rb @@ -153,14 +153,8 @@ module Net raise ArgumentError, "invalid option(s): #{invalid_options.join(', ')}" end - files = case options.fetch(:config, true) - when true then Net::SSH::Config.default_files - when false, nil then [] - else Array(options[:config]) - end - options[:user] = user if user - options = Net::SSH::Config.for(host, files).merge(options) + options = configuration_for(host, options.fetch(:config, true)).merge(options) host = options.fetch(:host_name, host) if !options.key?(:logger) @@ -196,5 +190,24 @@ module Net raise AuthenticationFailed, user end end + + # Returns a hash of the configuration options for the given host, as read + # from the SSH configuration file(s). If +use_ssh_config+ is true (the + # default), this will load configuration from both ~/.ssh/config and + # /etc/ssh_config. If +use_ssh_config+ is nil or false, nothing will be + # loaded (and an empty hash returned). Otherwise, +use_ssh_config+ may + # be a file name (or array of file names) of SSH configuration file(s) + # to read. + # + # See Net::SSH::Config for the full description of all supported options. + def self.configuration_for(host, use_ssh_config=true) + files = case use_ssh_config + when true then Net::SSH::Config.default_files + when false, nil then return {} + else Array(use_ssh_config) + end + + Net::SSH::Config.for(host, files) + end end end -- cgit v1.2.1