Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Name resolution module

by VinsWorldcom (Parson)
on Oct 03, 2017 at 11:24 UTC ( #1200603=perlquestion: print w/replies, xml ) Need Help??
VinsWorldcom has asked for the wisdom of the Perl Monks concerning the following question:

This seems like a basic question, but I can't find what I'm looking for.

I'm looking for a module that does DNS name resolution *easy*. I can write the code to do getaddrinfo() / getnameinfo() and the legacy gethostby...() but that's lots of code I cut/paste into my scripts whenever I need name resolution. To make it worse, there are all sorts of checks for Socket version to accommodate for IPv6 - which I need my scripts to be aware of.

I'm looking for something like:

use Magic::Name::Resolution ... host2addr(''); # print 1st IP addr returned, 4 or 6 host2ipv4(''); # print 1st IPv4 addr returned host2ipv6(''); # print 1st IPv6 addr returned ...

Hopefully you get the picture. All the messy stuff I do in code to resolve those names to addresses is obscured in a module. I thought of putting all my code in a module, but wanted to see what you all use since my CPAN searches for "DNS", "resolve", "host", "getaddr" turn up the usual suspects which require more "low-level" code writing (which again, I do, but I do repeatedly in all my scripts that need it). Looking for a nice simple API like above in a re-usable module instead of my current cut/paste approach.

Replies are listed 'Best First'.
Re: Name resolution module
by hippo (Canon) on Oct 03, 2017 at 12:17 UTC
    All the messy stuff I do in code to resolve those names to addresses is obscured in a module. I thought of putting all my code in a module, but wanted to see what you all use

    TBH, with Net::DNS it is relatively simple so I've never bothered to code up a wrapper:

    #!/usr/bin/env perl use strict; use warnings; use Net::DNS; my $res = Net::DNS::Resolver->new (); print "Any: " . $res->query('')->pop('answer')->rdstrin +g . "\n"; print "IPv4: " . $res->query('', 'A')->pop('answer')->rd +string . "\n"; print "IPv6: " . $res->query('', 'AAAA')->pop('answer')- +>rdstring . "\n";

    The advantage is that if you are interested in any other aspects of the responses or the resolver those are easily obtainable/settable.

      Now that's interesting!! ++ I hadn't seen Net::DNS used that way. That could be what I'm looking for - thanks for the helpful usage; I'll have a deeper look.

Re: Name resolution module
by 1nickt (Monsignor) on Oct 03, 2017 at 11:53 UTC

    Hi, I'm certain that you know far more than I do about networking, but have you tried Net::Nslookup which wraps Net::DNS::Resolver?

    $ perl -Mstrict -MNet::Nslookup -E 'say scalar nslookup " +m"'
    Not sure about IPv4 vs IPv6, but maybe you can extend it/steal the good parts?

    Also note that (as you probably know) Google and others use load-balanced DNS so you will likely get a different result each time you query.

    Hope this helps!

    The way forward always starts with a minimal test.

      Thanks. I had seen Net::DNS::Resolver which is under Net::DNS. It just didn't provide the varied output I'm looking for - at least I think not - between IPv4/6 and reverse lookups. You need to specify the address family into the routine if I remember correctly.

      I'll have a look at Net::NSLookup.

Re: Name resolution module
by holyghost (Sexton) on Oct 04, 2017 at 19:11 UTC
    If you want to write it yourself read, if you want a Scheme version I have done such things in sunterlib. It depends if you want to write it yourself.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1200603]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-06-22 07:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (122 votes). Check out past polls.