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

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 ddlookup.pl wbur.org Can't resolve : No such file or directory

Comment on DNS Lookup
Download Code
Re: DNS Lookup
by toolic (Chancellor) on Jan 19, 2012 at 19:34 UTC
    Change:
    $userinput = $name;
    to:
    $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;

      Ahh, life makes sense again.
      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.
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.

    Socket

    #!/usr/bin/perl -l use strict; use warnings; use IO::Socket; use Socket qw(SOCK_STREAM getaddrinfo); my $hostname = 'www.perlmonks.org'; 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"; }

    Net::DNS

    #!/usr/bin/perl use strict; use warnings; use Net::DNS; my $host = 'www.perlmonks.org'; 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://948837]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-08-01 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (50 votes), past polls