# File lib/ghost/mac-host.rb, line 19 def add(host, ip = "127.0.0.1", force = false) if find_by_host(host).nil? || force unless ip[%r^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/] ip = Socket.gethostbyname(ip)[3].bytes.to_a.join('.') end %x#{CreateCmd % [host, ip]}` flush! find_by_host(host) else raise Ghost::RecordExists, "Can not overwrite existing record" end end
# File lib/ghost/mac-host.rb, line 58 def delete(host) %x#{DeleteCmd % host.to_s}` flush! end
# File lib/ghost/mac-host.rb, line 63 def delete_matching(pattern) pattern = Regexp.escape(pattern) hosts = list.select { |h| h.to_s.match(%r#{pattern}/) } hosts.each do |h| delete(h) end flush! unless hosts.empty? hosts end
# File lib/ghost/mac-host.rb, line 53 def empty! list.each { |h| delete(h) } nil end
# File lib/ghost/mac-host.rb, line 33 def find_by_host(host) @hosts ||= {} @hosts[host] ||= begin output = %x#{ReadCmd % host}` if output =~ %reDSRecordNotFound/ nil else host = parse_host(output) ip = parse_ip(output) Host.new(host, ip) end end end
# File lib/ghost/mac-host.rb, line 49 def find_by_ip(ip) nil end
Flushes the DNS Cache
# File lib/ghost/mac-host.rb, line 74 def flush! %xdscacheutil -flushcache` @hosts = {} true end
# File lib/ghost/mac-host.rb, line 13 def list list = %x#{ListCmd}` list = list.split("\n") list.collect { |host| Host.new(host.chomp) } end
# File lib/ghost/mac-host.rb, line 95 def initialize(host, ip=nil) @host = host @ip = ip end
# File lib/ghost/mac-host.rb, line 81 def parse_host(output) parse_value(output, 'RecordName') end
# File lib/ghost/mac-host.rb, line 85 def parse_ip(output) parse_value(output, 'IPAddress') end
# 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
# File lib/ghost/linux-host.rb, line 153 def surround_with_tokens(str) "\n#{@@start_token}\n#{str}#{@@end_token}\n" end
# 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
# 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 =~ %r^#{@@start_token}/ in_ghost_area, seen_tokens = true,true output << line << new_ghosts elsif line =~ %r^#{@@end_token}/ 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
# File lib/ghost/linux-host.rb, line 13 def ==(other) @host == other.host && @ip = other.ip end
# File lib/ghost/mac-host.rb, line 100 def hostname @host end
# File lib/ghost/mac-host.rb, line 107 def ip @ip ||= self.class.send(:parse_ip, dump) end