Michael Robinton > Net-DNS-Dig > Net::DNS::Dig



Annotate this POD


New  1
Open  1
View/Report Bugs
Module Version: 0.12   Source  


Net::DNS::Dig - dig like methods


IO methods and functions to return DNS information

These functions do not use SIG_ALRM and are safe to use in CGI programs.

  use Net::DNS::Dig qw(

  $dig = new Net::DNS::Dig();
  $dig = new Net::DNS::Dig( %hash );
  $dig = new Net::DNS::Dig( \%hash );

  $dobj = $dig->for(name, type);
  $tobj = $dig->to_text();

  $array_ptr =  $dig->data($section);
  $array_ptr = $tobj->data($section);

  $array_ptr =  $dig->records($type,$sect);
  $array_ptr = $tobj->records($type,$sect);

  $text  = $dig->sprintf;

  $rv    = $dig->rcode(true/false);
  $rdata =  $dig->rdata($type,$sect);
  $rdata = $tobj->rdata($type,$sect;

     or in array context
  @array = $dig->data($section);
  @array = $dig->records($type,$sect);
  @rdata = $dig->rdata($type,$sect);

        = ndd_gethostbyaddr( naddr_naddr6 );

        = ndd_gethostbyname( name_ipV4_ipV6 );

        = ndd_gethostbyname2( name_ipV4_ipV6 AF_family);

    or in array context
  $name    = ndd_gethostbyaddr(netaddr);
  $netaddr = ndd_gethostbyname(name_ipV4);
  $netaddr = ndd_gethostbyname2(name_ipV4_ipV6,AF_family);


These functions use or emulate the underlying system calls of the same name, enhancing the capability of Perl to support IPv6 where needed. If the function is present in Perl and/or Socket6, the Perl function is called directly.

If use Net::DNS::Dig qw( :forceEmu ) is loaded with the force emulation tag, the Net::DNS;Dig's version of the function is always used. This is useful when the underlying system gethostbyname2 and/or gethostbyaddr functions are present but broken.

NOTE: the emulations do not check NIS or system hosts file.

  Function Net::DNS::Dig (ndd_) gethostbyname

  input:        $name           text string or ip address
    [optional]  $timeout        seconds

ndd_gethostbyname emulates or uses Perl's gethostbyname system call.

  Function Net::DNS::Dig (ndd_) gethostbyname2

  input:        $name           text string or ip address
    [optional]  $AF_family      address family type
    [optional]  $timeout        seconds for the emulation

ndd_gethostbyname2 provides a fully functional gethostbyname2 implementation that will work on systems that do not support IPv6 or have broken IPv6 socket libraries.

If $AF_family is false, ndd_gethostbyname2 will examine the supplied $name to try and determine the appropriate AF_family if the name is an IPv4 address of the form d+.d+.d+.d+ or and IPv6 address in one of the RFC1884 formats it will do the right thing and return a text version of the address. Otherwise, AF_INET is assumed by default and a DNS lookup will be performed.

  Function Net::DNS::Dig (ndd_) gethostbyaddr

  input:        $naddr          network address
    [optional]  $AF_family      address family type
    [optional]  $timeout        seconds for the emulation

If $AF_family is false, ndd_gethostbyaddr will examine the supplied $naddr and determine the appropriate AF_family.

Common return, all functions

  $name         text name
  $aliases      space separated list of text names
  $atype        address type - AF_family constant
  $len          length of the address 4 or 16
  @addrs        array of naddrs in network form

These two functions return the constant value for the AF_families, respectively, of the underlying operating system.


Example usage of Net::DNS::Dig

  ########### example 1 retrieving netaddrs

  use NetAddr::IP::Util qw(inet_ntoa);
  use Net::DNS::Dig;

  $name = 'gmail.com';

  # return one of the gmail 'A' records

  $netaddr = Net::DNS::Dig->new()->for($name)->rdata();

  print inet_ntoa($netaddr),"\n";

  ########### example 2 retrieve many netaddrs

  use NetAddr::IP::Util qw(inet_ntoa);
  use Net::DNS::Dig;

  $name = 'gmail.com';

  # return all of the gmail 'A' records

  @netaddrs = Net::DNS::Dig->new()->for( $name )->rdata();

  foreach ( @netaddrs ) {
    print inet_ntoa( $_ ),"\n";

  ########### example 3 retrieve MX host for email

  use Net::DNS::Dig;

  $email = 'john.doe@gmail.com';

  ( $name = $email ) =~ s/.+\@(.+)/$1/;

  # return all of the gmail 'MX' records
  # records return PRIORITY, HOST, ...   
  # hostnames are unique

  my %mx_info = reverse Net::DNS::Dig->new()->for( $name,'MX' )->rdata();

  my @host_by_priority;

  foreach ( sort {
        $mx_info{$a} <=> $mx_info{$b}
                 } keys %mx_info ) {
    push @host_by_priority, $_;

  foreach ( @host_by_priority ) {  
    print "$_\t    $mx_info{$_}\n";

  ########### example 4 a simple 'dig' script

  # example simple 'dig.pl' script
  use Net::DNS::Dig;

  my ($name,$type);

  while ( $_ = shift @ARGV ) {
    if ( $_ eq '-t' ) {
      $type = shift;
    } else {
      $name = $_;

  print Net::DNS::Dig->new()->for( $name, $type )->sprintf;

# end of script simple dig.pl

  command prompt > dig.pl -t aaaa arpa.com


  ; <<>> Net::DNS::Dig 0.01 <<>> -t aaaa arpa.com.
  ;; Got answer.
  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35979
  ;; flags: qr ra; QUERY: 1, ANSWER: 0, AUTHORITY: 8, ADDITIONAL: 8

  ;arpa.com.              IN      AAAA

  arpa.com.       79436   IN      NS       pdns2.ultradns.net.
  arpa.com.       79436   IN      NS       pdns1.ultradns.net.
  arpa.com.       79436   IN      NS       pdns5.ultradns.info.
  arpa.com.       79436   IN      NS       pdns3.ultradns.org.
  arpa.com.       79436   IN      NS       udns2.ultradns.net.
  arpa.com.       79436   IN      NS       pdns4.ultradns.org.
  arpa.com.       79436   IN      NS       udns1.ultradns.net.
  arpa.com.       79436   IN      NS       pdns6.ultradns.co.uk.

  pdns1.ultradns.net.     54013   IN      A
  pdns2.ultradns.net.     54013   IN      A
  pdns3.ultradns.org.     67699   IN      A
  pdns4.ultradns.org.     67699   IN      A
  pdns5.ultradns.info.    67699   IN      A
  pdns6.ultradns.co.uk.   67699   IN      A
  udns1.ultradns.net.     67698   IN      A
  udns2.ultradns.net.     67698   IN      A

  ;; Query time: 27 ms
  ;; SERVER: 53(
  ;; WHEN: Mon Oct 10 17:23:39 2011
  ;; MSG SIZE rcvd: 365 -- XFR size: 17 records

  ########### example 5 a complex 'dig' script

  # example complex 'dig.pl' script
  use Net::DNS::Dig qw(

  my($name, $type, $port, $server, $tcp, $time, $recurse);

  unless (@ARGV) {
    print qq|\nusage: $0 [options] name

        -t [type]               a, mx, etc...
        -p [port number]
        +tcp                    use TCP
        +time=[seconds]         timeout


  while ( $_ = shift @ARGV ) {
    if ( $_ eq '-t' ) {
      $type = shift;
    elsif ( $_ eq '-p' ) {
      $port = shift;
    elsif ( $_ =~ /^\@(.+)/ ) {
      $server = $1;
    elsif ( lc $_ eq '\+tcp' ) {
      $tcp = 'tcp';
    elsif ( $_ =~ /^\+time=(\d+)/ ) {
      $time = $1;
    elsif ( $_ =~ /^\+norecursive/ ) {
      $recurse = 1;
    else {
      $name = $_;

  my $config = {
        Timeout   => $time,
        PeerAddr  => $server,
        PeerPort  => $port,
        Proto     => $tcp,
        Recursion => $recurse,
  print Net::DNS::Dig->new($config)->for($name,$type)->to_text->sprintf;

  # end of script complex dig.pl




Michael Robinton <michael@bizsystems.com>

COPYRIGHT 2011-2014 ^

Michael Robinton <michael@bizsystems.com>

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of either:

  a) the GNU General Public License as published by the Free
  Software Foundation; either version 2, or (at your option) any
  later version, or

  b) the "Artistic License" which comes with this distribution.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the Artistic License for more details.

You should have received a copy of the Artistic License with this distribution, in the file named "Artistic". If not, I'll be glad to provide one.

You should also have received a copy of the GNU General Public License along with this program in the file named "Copying". If not, write to the

        Free Software Foundation, Inc.
        59 Temple Place, Suite 330
        Boston, MA  02111-1307, USA

or visit their web page on the internet at:


See also: ^

Net::DNS::Codes(3), Net::DNS::ToolKit(3), Net::DNS::ToolKit::Utilities(3), NetAddr::IP::Util(3)

syntax highlighting: