Class: Host
- Inherits:
-
Object
- Object
- Host
- 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
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
Class Method Summary collapse
- .add(host, ip = "127.0.0.1", force = false) ⇒ Object
- .delete(name) ⇒ Object
- .delete_matching(pattern) ⇒ Object
- .empty! ⇒ Object
- .find_by_host(hostname) ⇒ Object
- .find_by_ip(ip) ⇒ Object
-
.flush! ⇒ Object
Flushes the DNS Cache.
- .list ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(host, ip) ⇒ Host
constructor
A new instance of Host.
Constructor Details
#initialize(host, ip) ⇒ Host
Returns a new instance of Host.
94 95 96 97 |
# File 'lib/ghost/mac-host.rb', line 94 def initialize(host, ip=nil) @host = host @ip = ip end |
Instance Attribute Details
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
106 107 108 |
# File 'lib/ghost/mac-host.rb', line 106 def ip @ip ||= self.class.send(:parse_ip, dump) end |
Class Method Details
.add(host, ip = "127.0.0.1", force = false) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ghost/mac-host.rb', line 18 def add(host, ip = "127.0.0.1", force = false) if find_by_host(host).nil? || force unless ip[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/] ip = Socket.gethostbyname(ip)[3].bytes.to_a.join('.') end `#{CreateCmd % [host, ip]}` flush! find_by_host(host) else raise "Can not overwrite existing record" end end |
.delete(name) ⇒ Object
57 58 59 60 |
# File 'lib/ghost/mac-host.rb', line 57 def delete(host) `#{DeleteCmd % host.to_s}` flush! end |
.delete_matching(pattern) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/ghost/mac-host.rb', line 62 def delete_matching(pattern) pattern = Regexp.escape(pattern) hosts = list.select { |h| h.to_s.match(/#{pattern}/) } hosts.each do |h| delete(h) end flush! unless hosts.empty? hosts end |
.empty! ⇒ Object
52 53 54 55 |
# File 'lib/ghost/mac-host.rb', line 52 def empty! list.each { |h| delete(h) } nil end |
.find_by_host(hostname) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ghost/mac-host.rb', line 32 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
48 49 50 |
# File 'lib/ghost/mac-host.rb', line 48 def find_by_ip(ip) nil end |
.flush! ⇒ Object
Flushes the DNS Cache
73 74 75 76 77 |
# File 'lib/ghost/mac-host.rb', line 73 def flush! `dscacheutil -flushcache` @hosts = {} true 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 |