Skip to content

Instantly share code, notes, and snippets.

@hamstar
Created May 1, 2013 17:47

Revisions

  1. hamstar created this gist May 1, 2013.
    24 changes: 24 additions & 0 deletions dnsserv.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/usr/bin/env ruby

    require 'rubygems'
    require 'rubydns'

    $R = Resolv::DNS.new
    Name = Resolv::DNS::Name
    IN = Resolv::DNS::Resource::IN
    Hosts = "/etc/dnsserv/hosts"

    RubyDNS::run_server(:listen => [[:udp, "0.0.0.0", 5300]]) do
    # For this exact address record, return an IP address
    match(/.*/) do |transaction|
    line = `grep "#{transaction.name}" #{Hosts}`.split("\n").first
    transaction.passthrough!($R) if $?.exitstatus != 0
    ip = line.split("\t").first
    transaction.respond!(ip)
    end

    # Default DNS handler
    otherwise do |transaction|
    transaction.passthrough!($R)
    end
    end
    2 changes: 2 additions & 0 deletions hosts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    10.0.0.80 dev.test.org
    8.8.8.8 dns.gg.com