# Using resolv to get the mailhost

**URL:** <https://www.ruby-forum.com/t/using-resolv-to-get-the-mailhost/70367>\
**Category:** Ruby\
**Created:** [September 28, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/using-resolv-to-get-the-mailhost/70367 "2006-09-28T19:07:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [September 28, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/using-resolv-to-get-the-mailhost/70367/1 "2006-09-28T19:07:00Z")

</div>

Hi,

I’m trying to figure out a way to get the mailhost using the ‘resolv’  
library, but I’m having trouble because it isn’t documented. What’s a  
good analogue of this Perl script?

use strict;  
use Net::DNS;

my $resolv = Net::DNS::Resolver-\>new();  
my $packet = $resolv-\>search(“mailhost”);

foreach my $rr ($packet-\>answer){  
$rr-\>print;  
}

# Prints the following (IP addresses changed to protect the guilty):

[mailhost.foo.com](http://mailhost.foo.com). 900 IN CNAME [mailgate.domain.com](http://mailgate.domain.com).  
[mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.4  
[mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.5  
[mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.6  
[mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.7  
[mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.8  
[mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.9

I’d really just like the first name of the first answer, i.e.  
“[mailhost.foo.com](http://mailhost.foo.com)”.

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [September 29, 2006, 2:38am UTC](https://www.ruby-forum.com/t/using-resolv-to-get-the-mailhost/70367/2 "2006-09-29T02:38:20Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Daniel B. wrote:

> my $packet = $resolv-\>search(“mailhost”);  
> [mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.6  
> [mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.7  
> [mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.8  
> [mailgate.domain.com](http://mailgate.domain.com). 900 IN A 1.2.3.9
> 
> I’d really just like the first name of the first answer, i.e.  
> “[mailhost.foo.com](http://mailhost.foo.com)”.

It seems that I needed to put an explicit domain entry for [foo.com](http://foo.com) in my  
/etc/resolv.conf file. Once I did that, then  
Resolv.getaddress(“mailhost”) worked.

However, I’m confused as to why “host mailhost” and the Perl code above  
worked without it, while the Ruby code required it. Any insight  
appreciated.

Regards,

Dan

PS - Solaris 10, btw.

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (GNU/Linux)  
Comment: Using GnuPG with SUSE - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFHGqK3p/dorzCFX0RAuajAKCXN6zBl+4mkV4gtpKgfb2HtYHywwCglLYc  
9bD7X4QYv8uiIYVLeQwguBA=  
=lKyF  
-----END PGP SIGNATURE-----
