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

Re: DNS Lookup

by Khen1950fx (Canon)
on Jan 20, 2012 at 10:09 UTC ( #948921=note: print w/replies, xml ) Need Help??

in reply to DNS Lookup

I'm familiar with the information that you have read. It was almost cool 10 years ago, but time marches on. As an experiment, here are two scripts: one uses Socket, and the second one uses Net::DNS. You'll find that Socket isn't satisfactory.


#!/usr/bin/perl -l use strict; use warnings; use IO::Socket; use Socket qw(SOCK_STREAM getaddrinfo); my $hostname = ''; my ($err, @res) = getaddrinfo($hostname, "", {socktype => SOCK_STREAM}); die "Cannot getaddrinfo - $err" if $err; foreach my $ai (@res) { my ($err, $ipaddr) = getnameinfo($ai->{'addr'}, 'NI_NUMERICHOST', 'NIx_NOSERV'); die "Cannot getnameinfo - $err" if $err; print "$ipaddr"; }


#!/usr/bin/perl use strict; use warnings; use Net::DNS; my $host = ''; my $res = Net::DNS::Resolver->new; my $query = $res->search($host); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; print $rr->address, "\n"; } } else { warn "Query failed: ", $res->errorstring; }

Replies are listed 'Best First'.
Re^2: DNS Lookup
by jaldama (Acolyte) on Jan 20, 2012 at 21:01 UTC
    Socket did the trick for now but I did have Net::DNS in mind and will keep it there for more robust projects