Skip to content

Instantly share code, notes, and snippets.

@hamstar
Created May 1, 2013 17:47
Show Gist options
  • Save hamstar/5496892 to your computer and use it in GitHub Desktop.
Save hamstar/5496892 to your computer and use it in GitHub Desktop.
Simple DNS server in ruby. Tested and it works!
#!/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
10.0.0.80 dev.test.org
8.8.8.8 dns.gg.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment