# DNS query

**URL:** <https://www.ruby-forum.com/t/dns-query/178522>\
**Category:** Rails\
**Created:** [November 5, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/dns-query/178522 "2009-11-05T23:09:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [November 5, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/dns-query/178522/1 "2009-11-05T23:09:37Z")

</div>

Hi,

This is a bit of Ruby and a bit of Linux, but because I’m working on  
this in a Rails environment, I’ve decided to post it here.

I use the net-dns gem to make SOA queries like the following:

def query\_domain(domain)  
require ‘Net/DNS’  
res = Net::DNS::Resolver.new  
result = res.query(domain, ‘SOA’)  
return result.answer  
end

This works fine and I’m happy with the results. However, this is part  
of a server management setup that changes values in certain  
nameservers. We also want to monitor these changes, but most of these  
nameservers have a TTL of 4 hours for their domains. We can’t have the  
system wait for 4 hours before checking whether the procedure worked,  
so I need a way to get the server at the other end to return the  
result again, just a few seconds later. Is this possible? Is there a  
way to make the queried DNS flush the results and request them again?

Thank you very much.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [November 5, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/dns-query/178522/2 "2009-11-05T23:57:03Z")

</div>

Quoting jhaagmans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> res = Net::DNS::Resolver.new  
> result again, just a few seconds later. Is this possible? Is there a  
> way to make the queried DNS flush the results and request them again?

How about just querying the authorative nameservers directly with:

res = Net::DNS::Resolver.new  
res.nameserver = ‘…’  
result = res.query(domain, Net::DNS::SOA)

or even (if I understand the docs right)

return Net::DNS::Resolver.new(:nameservers =\> ‘…’).query(domain,  
Net:🧬:SOA).answer

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [November 6, 2009, 1:46pm UTC](https://www.ruby-forum.com/t/dns-query/178522/3 "2009-11-06T13:46:28Z")

</div>

Multiple reasons. First, we don’t really want to store the nameservers  
as they might change alot, so we’d have to query our DNS for the  
nameservers and then query the NS itself. After we do that, the NS  
will return the same values for the time specified in the TTL. Second,  
the above won’t really work, it will just add the nameserver to the  
default DNS list as specified in /etc/hosts, so you’d have to tell it  
not to use /etc/hosts first. However, we sometimes do want to do that,  
so it would be better if we could tell our own DNS (that we don’t  
control) not to send cached values, but query for new ones.

Thank you for your response though. The one-line-example is in fact  
what we do, but not as readable 😉

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [November 6, 2009, 10:11pm UTC](https://www.ruby-forum.com/t/dns-query/178522/4 "2009-11-06T22:11:04Z")

</div>

Well, let’s forget the above. I’d have to control the DNS server and  
make sure it doesn’t cache, that’s not possible. Let’s focus on what I  
actually want to do: I want to monitor a domain for responsiveness.  
But I want to do it more than once an hour and I want to make sure  
nothing in between will return a cached response instead of the actual  
response.

So I’ve written this method:

def make\_request(domain)  
response = Net::HTTP.new(domain).request\_get(’/’)  
if !response.nil?  
domain.update\_attribute(:last\_up, Time.now)  
end  
end

It works. Once. After that, Net:HTTP.new(domain) will just give me the  
cached information and I don’t want that. I want to monitor it! If it  
goes down, I want an empty response. How can I do that?

Thank you.
