Created
February 8, 2018 15:04
-
-
Save atatarn/6451f533df61228ee169480a872ef40b to your computer and use it in GitHub Desktop.
Perl script to pass thru all domain's CNAMEs and find resulting IPs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl -w | |
| use strict; | |
| use Net::DNS; | |
| if ($ARGV[0]) { | |
| gptr_a($ARGV[0]); | |
| } else { | |
| die "Usage: $0 HOSTNAME\n"; | |
| } | |
| # 1. получаем имя хоста, посмотреть, что днс ответит на запрос по нему | |
| # ответ $query->answer если А-запись есть: Net::DNS::RR::A=HASH(0x8016d1738) | |
| # в случае с CNAME: | |
| #Net::DNS::RR::CNAME=HASH(0x8016d1738) | |
| #Net::DNS::RR::CNAME=HASH(0x8010f1ba0) | |
| #Net::DNS::RR::A=HASH(0x8010f1d50) | |
| # 2. в любом случае, сначала пойдут все cname, потом все A | |
| # их и распихиваем по массивам @cnames и @ips | |
| # 3. резолвим айпишники в хосты и выводим список цнеймов и ip: host | |
| sub gptr_a { | |
| my $qhost = $_[0]; | |
| my $res = Net::DNS::Resolver->new; | |
| my $query = $res->query($qhost, 'A'); | |
| push(my @cnames, $qhost); | |
| my @ips; | |
| my %aptr; | |
| if ($query) { | |
| foreach my $rr ($query->answer) { | |
| my $amt; | |
| if ($rr =~ /Net::DNS::RR::CNAME=HASH(.+)/) { | |
| $amt = 'cname'; | |
| push(@cnames, $rr->$amt); | |
| } elsif ($rr =~ /Net::DNS::RR::A=HASH(.+)/) { | |
| $amt = 'address'; | |
| push(@ips, $rr->$amt); | |
| } | |
| } | |
| } else { | |
| print $res->errorstring, "\n"; | |
| } | |
| foreach (@ips) { | |
| if (/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/) { # if $_ has IP | |
| my @ptrs = ip2ptrs($_); | |
| my $ptrs = join ' ', @ptrs; | |
| $aptr{$_} = $ptrs; | |
| } else { | |
| $aptr{$_} = 'Unable to resolve'; | |
| } | |
| } | |
| if (@cnames > 1) { | |
| my $cnames = join ' -> ', @cnames; | |
| printf "%1s CNAMES: ${cnames}\n", ''; | |
| } | |
| foreach my $key (sort keys %aptr) { | |
| printf "%15s: $aptr{$key}\n", $key; | |
| } | |
| } | |
| sub ip2ptrs { | |
| my $qstring = $_[0]; | |
| my $qtype = 'PTR'; | |
| my $amt = 'ptrdname'; | |
| my @answrs; | |
| my $res = Net::DNS::Resolver->new; | |
| my $query = $res->query($qstring, $qtype); | |
| if ($query) { | |
| foreach my $rr ($query->answer) { | |
| push(@answrs, $rr->$amt); | |
| } | |
| } else { | |
| push(@answrs, $res->errorstring); | |
| } | |
| @answrs; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment