Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

DNS Lookup

by jaldama (Acolyte)
on Jan 19, 2012 at 19:28 UTC ( #948837=perlquestion: print w/replies, xml ) Need Help??
jaldama has asked for the wisdom of the Perl Monks concerning the following question:

Howdy! I've spent some time reading about looking up a DNS in perl but am having trouble with my script. Hoping someone could give me a hint as to where I've gone wrong?

#!/usr/bin/perl-w use Socket; if ( $#ARGV != 0 ) { print "Usage: $0 hostname\n"; exit(1); } $userinput = $ARGV[0]; if ($userinput =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ ) { $userinput = $ip; $hostname = gethostbyaddr(inet_aton($ip), AF_INET) or die "Can't R +esolve $ip: $!\n"; print $hostname; } else { $userinput = $name; $ipaddr = gethostbyname($name) or die "Can't resolve $name: $!\n"; $ipaddr = inet_ntoa(inet_aton($name)); print $ipaddr; } ##prints this? $ perl Can't resolve : No such file or directory

Replies are listed 'Best First'.
Re: DNS Lookup
by toolic (Bishop) on Jan 19, 2012 at 19:34 UTC
    $userinput = $name;
    $name = $userinput;

    I think you were missing the warning that I see because your shebang line is messed up (add a space before the -w), and you are probably bypassing it anyway by invoking your script with the perl executable on the command line. Also add:

    use warnings;
    use strict and warnings

    The same goes for $userinput = $ip;

      you are probably bypassing it anyway by invoking your script with the perl executable on the command line.
      Perl actually honors command line switches on the she bang line, even when invoked directly:
      $ cat xxx #!/usr/bin/perl -w print undef; $ perl xxx Use of uninitialized value in print at xxx line 3.

        Yes, for the most part :)

        $ perl #!/usr/bin/perl -Mstrict -- Too late for "-Mstrict" option at - line 1.
      Ahh, life makes sense again.
Re: DNS Lookup
by Khen1950fx (Canon) on Jan 20, 2012 at 10:09 UTC
    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; }
      Socket did the trick for now but I did have Net::DNS in mind and will keep it there for more robust projects

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://948837]
Approved by marto
[Corion]: A good morning, choroba!

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

    Results (117 votes). Check out past polls.