Class: Host

Inherits:
Object
  • Object
show all
Defined in:
lib/ghost/mac-host.rb,
lib/ghost/linux-host.rb

Constant Summary collapse

ListCmd =
"dscl localhost -list /Local/Default/Hosts 2>&1"
ReadCmd =
"dscl localhost -read /Local/Default/Hosts/%s 2>&1"
CreateCmd =
"sudo dscl localhost -create /Local/Default/Hosts/%s IPAddress %s 2>&1"
DeleteCmd =
"sudo dscl localhost -delete /Local/Default/Hosts/%s 2>&1"
@@hosts_file =
'/etc/hosts'
@@permanent_hosts =
[Host.new("localhost",      "127.0.0.1"),
Host.new(`hostname`.chomp, "127.0.0.1")]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, ip) ⇒ Host

Returns a new instance of Host.



78
79
80
81
# File 'lib/ghost/mac-host.rb', line 78

def initialize(host, ip=nil)
  @host = host
  @ip = ip
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



90
91
92
# File 'lib/ghost/mac-host.rb', line 90

def ip
  @ip ||= self.class.send(:parse_ip, dump)
end

Class Method Details

.add(host, ip = "127.0.0.1", force = false) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ghost/mac-host.rb', line 16

def add(host, ip = "127.0.0.1", force = false)
  if find_by_host(host).nil? || force
    `#{CreateCmd % [host, ip]}`
    flush!
    find_by_host(host)
  else
    raise "Can not overwrite existing record"
  end      
end

.delete(name) ⇒ Object



51
52
53
54
# File 'lib/ghost/mac-host.rb', line 51

def delete(host)
  `#{DeleteCmd % host.to_s}`
  flush!
end

.empty!Object



46
47
48
49
# File 'lib/ghost/mac-host.rb', line 46

def empty!
  list.each { |h| delete(h) }
  nil
end

.find_by_host(hostname) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ghost/mac-host.rb', line 26

def find_by_host(host)
  @hosts ||= {}
  @hosts[host] ||= begin
    output = `#{ReadCmd % host}`
  
    if output =~ /eDSRecordNotFound/
      nil
    else
      host = parse_host(output)
      ip = parse_ip(output)
    
      Host.new(host, ip)
    end
  end
end

.find_by_ip(ip) ⇒ Object



42
43
44
# File 'lib/ghost/mac-host.rb', line 42

def find_by_ip(ip)
  nil
end

.flush!Object

Flushes the DNS Cache



57
58
59
60
61
# File 'lib/ghost/mac-host.rb', line 57

def flush!
  `dscacheutil -flushcache`
  @hosts = {}
  true
end

.listObject



10
11
12
13
14
# File 'lib/ghost/mac-host.rb', line 10

def list
  list = `#{ListCmd}`
  list = list.split("\n")
  list.collect { |host| Host.new(host.chomp) }
end

Instance Method Details

#==(other) ⇒ Object



9
10
11
# File 'lib/ghost/linux-host.rb', line 9

def ==(other)
  @host == other.host && @ip = other.ip
end