Method: Net::SSH::Config.load

Defined in:
lib/net/ssh/config.rb

.load(path, host, settings = {}) ⇒ Object

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/net/ssh/config.rb', line 59

def load(path, host, settings={})
  file = File.expand_path(path)
  return settings unless File.readable?(file)
  
  globals = {}
  matched_host = nil
  multi_host = []
  seen_host = false
  IO.foreach(file) do |line|
    next if line =~ /^\s*(?:#.*)?$/
    
    if line =~ /^\s*(\S+)\s*=(.*)$/
      key, value = $1, $2
    else
      key, value = line.strip.split(/\s+/, 2)
    end

    # silently ignore malformed entries
    next if value.nil?

    key.downcase!
    value = $1 if value =~ /^"(.*)"$/
    
    value = case value.strip
      when /^\d+$/ then value.to_i
      when /^no$/i then false
      when /^yes$/i then true
      else value
      end
    
    if key == 'host'
      # Support "Host host1 host2 hostN".
      # See http://github.com/net-ssh/net-ssh/issues#issue/6
      multi_host = value.to_s.split(/\s+/)
      matched_host = multi_host.select { |h| host =~ pattern2regex(h) }.first
      seen_host = true
    elsif !seen_host
      if key == 'identityfile'
        (globals[key] ||= []) << value
      else
        globals[key] = value unless settings.key?(key)
      end
    elsif !matched_host.nil?
      if key == 'identityfile'
        (settings[key] ||= []) << value
      else
        settings[key] = value unless settings.key?(key)
      end
    end
  end
  
  settings = globals.merge(settings) if globals
  
  return settings
end