Class: LinuxAdmin::Dns

Inherits:
Object
  • Object
show all
Defined in:
lib/linux_admin/dns.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = "/etc/resolv.conf") ⇒ Dns

Returns a new instance of Dns.



7
8
9
10
# File 'lib/linux_admin/dns.rb', line 7

def initialize(filename = "/etc/resolv.conf")
  @filename = filename
  reload
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



3
4
5
# File 'lib/linux_admin/dns.rb', line 3

def filename
  @filename
end

#nameserversObject

Returns the value of attribute nameservers.



4
5
6
# File 'lib/linux_admin/dns.rb', line 4

def nameservers
  @nameservers
end

#search_orderObject

Returns the value of attribute search_order.



5
6
7
# File 'lib/linux_admin/dns.rb', line 5

def search_order
  @search_order
end

Instance Method Details

#reloadObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/linux_admin/dns.rb', line 12

def reload
  @search_order = []
  @nameservers  = []

  File.read(@filename).split("\n").each do |line|
    if line =~ /^search .*/
      @search_order += line.split(/^search\s+/)[1].split
    elsif line =~ /^nameserver .*/
      @nameservers.push(line.split[1])
    end
  end
end

#saveObject



25
26
27
28
29
# File 'lib/linux_admin/dns.rb', line 25

def save
  search  = "search #{@search_order.join(" ")}\n" unless @search_order.empty?
  servers = @nameservers.collect { |ns| "nameserver #{ns}\n" }.join
  File.write(@filename, "#{search}#{servers}")
end