class SshdConfig::SshdConfig
Attributes
lines[R]
Public Class Methods
new(filename)
click to toggle source
# File lib/sshd_config.rb, line 5 def initialize(filename) @filename = filename @file = File.open(@filename, "r") end
read(filename)
click to toggle source
# File lib/sshd_config.rb, line 25 def self.read(filename) sshd_config = SshdConfig.new(filename) sshd_config.load return sshd_config end
Public Instance Methods
change_property(prop, value)
click to toggle source
# File lib/sshd_config.rb, line 36 def change_property(prop, value) prop = prop.gsub("\n", "").split(" ") ([prop[0]].concat value.split(" ")).join(" ") end
close()
click to toggle source
# File lib/sshd_config.rb, line 51 def close @file.close end
get_value(name)
click to toggle source
# File lib/sshd_config.rb, line 74 def get_value(name) prop = @lines.select do |line| (line[:type] == :property) and (property(line[:content])[:name].chomp.upcase == name.chomp.upcase) end if prop.length == 1 return property(prop[0][:content])[:value] else raise NoMethodError end end
load()
click to toggle source
# File lib/sshd_config.rb, line 10 def load @lines = [] @file.readlines.each do |line| @lines << {content: line, type: type(line)} end end
method_missing(name, *args, &block)
click to toggle source
# File lib/sshd_config.rb, line 85 def method_missing(name, *args, &block) return case name when /^.*=$/ then set_value(name.to_s[0..-2], args[0]) else get_value(name.to_s) end end
property(prop)
click to toggle source
# File lib/sshd_config.rb, line 31 def property(prop) content = prop.gsub("\n", "").split(" ") return {name: content[0], value: content[1..-1].join(' ')} end
save()
click to toggle source
# File lib/sshd_config.rb, line 41 def save @file.close @file = File.open(@filename, "w") @lines.each do |line| @file.write(line[:content]) end @file.close @file = File.open(@filename, "r") end
set_value(name, value)
click to toggle source
# File lib/sshd_config.rb, line 55 def set_value(name, value) prop = @lines.select do |line| (line[:type] == :property) and (property(line[:content])[:name].chomp.upcase == (name).chomp.upcase) end if prop.length == 1 new_value = change_property(prop[0][:content], value) @lines.each_with_index do |line, i| if line[:content] == prop[0][:content] @lines[i][:content] = "#{new_value}\n" end end return new_value else new_value = "#{name} #{value}\n" @lines << {content: new_value, type: :property} return new_value end end
type(line)
click to toggle source
# File lib/sshd_config.rb, line 17 def type(line) case line[0] when '#' then :comment when "\n" then :empty else :property end end