Net::DNS::Resolver 对象的 Data::Dumper 输出中的垃圾输出或随机字符

Junk output or random character in Data::Dumper output for Net::DNS::Resolver object

我正在熟悉 Perl 中的 Net::DNS 库,并且使用

创建了一个对象
my $res = Net::DNS::Resolver->new();

但是,尽管输出本身是正确的,但仅尝试查询域名会显示很多垃圾值。这是代码片段

#!/usr/bin/perl 

use Net::DNS;
use Net::IP;
use Data::Dumper;
my $rr;

$domain = 'google.com';
my $res = Net::DNS::Resolver->new();

my $ns_req = $res->query($domain, "NS");
print "\n\n@@@\n".Dumper($ns_req)."\n@@@\n\n";

以下是针对此对象测试的各种域的 2 个输出:

显示的这些垃圾值是什么?有没有办法稍微清理一下输出以便正确读取输出?

您正在转储对象的内部结构,其中包括保存原始响应字节的缓冲区。

您应该使用 module documentation 中定义的 API 来访问信息。

#!/usr/bin/env perl

use strict;
use warnings;

use Net::DNS;

my $resolver = Net::DNS::Resolver->new;

my $result = $resolver->query('google.com', "NS");
$result->print;

输出:

;; Answer received from x.x.x.x (100 bytes)
;; HEADER SECTION
;;      id = 39595
;;      qr = 1  aa = 0  tc = 0  rd = 1  opcode = QUERY
;;      ra = 1  z  = 0  ad = 0  cd = 0  rcode  = NOERROR
;;      qdcount = 1     ancount = 4     nscount = 0     arcount = 0
;;      do = 0

;; QUESTION SECTION (1 record)
;; google.com.  IN      NS

;; ANSWER SECTION (4 records)
google.com.     21599   IN      NS      ns4.google.com.
google.com.     21599   IN      NS      ns2.google.com.
google.com.     21599   IN      NS      ns1.google.com.
google.com.     21599   IN      NS      ns3.google.com.

;; AUTHORITY SECTION (0 records)

;; ADDITIONAL SECTION (0 records)

query 方法 returns 一个 Net::DNS::Packet 提供了其他方法来获取响应的特定部分。

例如:

#!/usr/bin/env perl

use strict;
use warnings;

use Net::DNS;

my $resolver = Net::DNS::Resolver->new;

my $result = $resolver->query('google.com', "NS");

for my $answer ($result->answer) {
    print $answer->nsdname, "\n";
}

输出:

ns2.google.com
ns1.google.com
ns3.google.com
ns4.google.com

如果您对二进制缓冲区的内容感兴趣,Net::DNS::Packet 有一个 data 方法可以 returns 该缓冲区的内容。正如 RFC 1035 指出的那样:

3.2. RR definitions

3.2.1. Format

All RRs have the same top level format shown below:

                                    1  1  1  1  1  1
      0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                                               |
    /                                               /
    /                      NAME                     /
    |                                               |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                      TYPE                     |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                     CLASS                     |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                      TTL                      |
    |                                               |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                   RDLENGTH                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
    /                     RDATA                     /
    /                                               /
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+


where:

NAME            an owner name, i.e., the name of the node to which this
                resource record pertains.

TYPE            two octets containing one of the RR TYPE codes.

CLASS           two octets containing one of the RR CLASS codes.

TTL             a 32 bit signed integer that specifies the time interval
                that the resource record may be cached before the source
                of the information should again be consulted.  Zero
                values are interpreted to mean that the RR can only be
                used for the transaction in progress, and should not be
                cached.  For example, SOA records are always distributed
                with a zero TTL to prohibit caching.  Zero values can
                also be used for extremely volatile data.

RDLENGTH        an unsigned 16 bit integer that specifies the length in
                octets of the RDATA field.

RDATA           a variable length string of octets that describes the
                resource.  The format of this information varies
                according to the TYPE and CLASS of the resource record.

您可以通过 hexdump 检查 $result->data 的内容:

#!/usr/bin/env perl                                                  

use strict;                                                          
use warnings;                                                        
use Net::DNS;                                                        

my $resolver = Net::DNS::Resolver->new;                              
my $result = $resolver->query('google.com', "NS");                   
print $result->data;                                                 
C:\...\t> perl tt.pl | xxd                               
00000000: 3256 8180 0001 0004 0000 0000 0667 6f6f  2V...........goo  
00000010: 676c 6503 636f 6d00 0002 0001 c00c 0002  gle.com.........  
00000020: 0001 0000 545f 0006 036e 7333 c00c c00c  ....T_...ns3....  
00000030: 0002 0001 0000 545f 0006 036e 7334 c00c  ......T_...ns4..  
00000040: c00c 0002 0001 0000 545f 0006 036e 7332  ........T_...ns2  
00000050: c00c c00c 0002 0001 0000 545f 0006 036e  ..........T_...n  
00000060: 7331 c00c                                s1..