jose_m has asked for the wisdom of the Perl Monks concerning the following question:

hello Monks, can someone help me query dns for a part of a hostname and return a list of hosts that match into an array? i can query using socket but it seems to only return the subdomain

#!/usr/bin/perl use Socket; use IO::Socket; use warnings; sub getWin { @win = gethostbyname("win") or die $!; print "@win\n"; } &getWin;

this is not retuning a list of hosts names win01.xx.com does anyone see the issue? also net::dns does not seem to be able to return a list of hostnames, is this true? thanks jose.

Replies are listed 'Best First'.
Re: query dns by hostname
by skx (Parson) on Mar 25, 2014 at 12:50 UTC

    This is a simple example - but note that you might be seeing failures because the host you're querying win isn't fully-qualified..?

    #!/usr/bin/perl use strict; use warnings; use Net::DNS; sub lookupHost { my( $host ) = ( @_ ); my @results; my $res = Net::DNS::Resolver->new; my $query = $res->search($host); if ($query) { foreach my $rr ($query->answer) { push( @results, $rr->address ) if ( $rr->type eq "A" ); } } else { warn "query failed: ", $res->errorstring, "\n"; } return( @results ); } my @out = lookupHost( "google.com" ); foreach my $ip ( @out ) { print "result: $ip\n"; }
    Steve
    --
Re: query dns by hostname
by lithron (Chaplain) on Mar 27, 2014 at 00:20 UTC
    I think you are misunderstanding what gethostbyname() does. gethostbyname allows you to resolve a DNS (or /etc/hosts entry) to an IP address. It does not allow you to do wildcard searches on a partial domain name.