class Ghost::Host
Constants
- CreateCmd
- DeleteCmd
- ListCmd
- ReadCmd
Attributes
host[R]
hostname[R]
ip[R]
name[R]
to_s[R]
Public Class Methods
new(host, ip)
click to toggle source
# File lib/ghost/linux-host.rb, line 8 def initialize(host, ip) @host = host @ip = ip end
Public Instance Methods
==(other)
click to toggle source
# File lib/ghost/linux-host.rb, line 13 def ==(other) @host == other.host && @ip = other.ip end
add(host, ip = "127.0.0.1", force = false)
click to toggle source
# File lib/ghost/linux-host.rb, line 51 def add(host, ip = "127.0.0.1", force = false) with_exclusive_file_access do if find_by_host(host).nil? || force hosts = list hosts = hosts.delete_if { |h| h.name == host } unless ip[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/] ip = Socket.gethostbyname(ip)[3].bytes.to_a.join('.') end new_host = Host.new(host, ip) hosts << new_host write_out!(hosts) new_host else raise Ghost::RecordExists, "Can not overwrite existing record" end end end
delete(name)
click to toggle source
# File lib/ghost/linux-host.rb, line 85 def delete(name) with_exclusive_file_access do hosts = list hosts = hosts.delete_if { |host| host.name == name } write_out!(hosts) end end
delete_matching(pattern)
click to toggle source
# File lib/ghost/linux-host.rb, line 93 def delete_matching(pattern) with_exclusive_file_access do pattern = Regexp.escape(pattern) hosts = list.select { |host| host.name.match(/#{pattern}/) } hosts.each { |host| delete(host.name) } hosts end end
empty!()
click to toggle source
# File lib/ghost/linux-host.rb, line 81 def empty! write_out!([]) end
find_by_host(hostname)
click to toggle source
# File lib/ghost/linux-host.rb, line 73 def find_by_host(hostname) list.find { |host| host.hostname == hostname } end
find_by_ip(ip)
click to toggle source
# File lib/ghost/linux-host.rb, line 77 def find_by_ip(ip) list.find_all{ |host| host.ip == ip } end
flush!()
click to toggle source
Flushes the DNS Cache
# File lib/ghost/mac-host.rb, line 74 def flush! %xdscacheutil -flushcache` @hosts = {} true end
list()
click to toggle source
# File lib/ghost/linux-host.rb, line 27 def list with_exclusive_file_access(true) do |file| entries = [] in_ghost_area = false file.pos = 0 file.each do |line| if !in_ghost_area and line =~ /^#{@@start_token}/ in_ghost_area = true elsif in_ghost_area if line =~ /^#{@@end_token}/o in_ghost_area = false elsif line =~ /^(\d+\.\d+\.\d+\.\d+)\s+(.*)$/ ip = $1 hosts = $2.split(/\s+/) hosts.each { |host| entries << Host.new(host, ip) } end end end entries end end
Protected Instance Methods
parse_host(output)
click to toggle source
# File lib/ghost/mac-host.rb, line 81 def parse_host(output) parse_value(output, 'RecordName') end
parse_ip(output)
click to toggle source
# File lib/ghost/mac-host.rb, line 85 def parse_ip(output) parse_value(output, 'IPAddress') end
parse_value(output, key)
click to toggle source
# File lib/ghost/mac-host.rb, line 89 def parse_value(output, key) match = output.match(Regexp.new("^#{key}: (.*)$")) match[1] unless match.nil? end
surround_with_tokens(str)
click to toggle source
# File lib/ghost/linux-host.rb, line 153 def surround_with_tokens(str) "\n#{@@start_token}\n#{str}#{@@end_token}\n" end
with_exclusive_file_access(read_only = false) { |_file| ... }
click to toggle source
# File lib/ghost/linux-host.rb, line 104 def with_exclusive_file_access(read_only = false) return_val = nil flag = read_only ? 'r' : 'r+' if @_file return_val = yield @_file else File.open(@@hosts_file, flag) do |f| f.flock File::LOCK_EX begin @_file = f return_val = yield f ensure @_file = nil end end end return_val end
write_out!(hosts)
click to toggle source
# File lib/ghost/linux-host.rb, line 125 def write_out!(hosts) with_exclusive_file_access do |f| new_ghosts = hosts.inject("") {|s, h| s + "#{h.ip} #{h.hostname}\n" } output = "" in_ghost_area, seen_tokens = false,false f.pos = 0 f.each do |line| if line =~ /^#{@@start_token}/o in_ghost_area, seen_tokens = true,true output << line << new_ghosts elsif line =~ /^#{@@end_token}/o in_ghost_area = false end output << line unless in_ghost_area end if !seen_tokens output << surround_with_tokens( new_ghosts ) end f.pos = 0 f.print output f.truncate(f.pos) end end
Private Instance Methods
dump()
click to toggle source
# File lib/ghost/mac-host.rb, line 112 def dump @dump ||= %x#{ReadCmd % hostname}` end