Subversion Repositories sysadmin_scripts

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
74 rodolico 1
#! /usr/bin/env perl
2
 
3
# https://perlmaven.com/checking-the-whois-record-of-many-domains
4
 
5
use strict;
6
use warnings;
7
 
8
use Data::Dumper;
9
use File::Basename;
10
 
11
my %results;
12
 
13
for ( my $i = 0; $i < scalar( @ARGV ); $i++ ) {
14
  my $data = '';
15
  my $domain = basename( $ARGV[$i], ( '.whois' ) );
16
  if ( open DATA,"<$ARGV[$i]" ) {
17
     $data = join( '', <DATA> );
18
  } else {
19
     warn "Could not read $ARGV[$i]: $!\n";
20
     next;
21
  }
22
  my @ns = get_ns($data);
23
 
24
  if ( @ns ) {
25
     foreach my $thisNS ( @ns ) {
26
        push @{ $results{ $thisNS } }, $domain;
27
     }
28
   } else {
29
      push  @{ $results{ 'Unknown' } }, $domain;
30
   }
31
}
32
 
33
foreach my $ns ( sort keys %results ) {
34
   print "$ns\t", join( "\n$ns\t", @{ $results{$ns} } ) . "\n";
35
}
36
 
37
 
38
sub get_ns {
39
  my ($data) = @_;
40
 
41
  my @ns;
42
 
43
  return ('Empty Record') unless $data; # this is a bad domain?
44
  return ('Invalid TLD' ) if $data =~ m/No whois server is known for this kind of object/;
45
 
46
  @ns = map { uc $_ } $data =~ /^\s*Name Server:\s*(\S+)\s*$/mg;
47
 
48
  if (not @ns) {
49
      @ns = map { uc $_ } $data =~ /^nserver:\s*(\S+)\s*$/mg;
50
  }
51
 
52
  if (not @ns) {
53
      my @lines = split /\n/, $data;
54
      return ('Expired Domain') if $lines[0] =~ m/^No Data Found/ || $lines[0] =~ m/^No match for/ || $lines[0] =~ m/^NOT FOUND/;
55
      my $in_ns = 0;
56
      for my $line (@lines) {
57
          if ($line =~ /^\s*Domain servers in listed order:\s*$/) {
58
              $in_ns = 1;
59
              next;
60
          }
61
          if ($line =~ /^\s*$/) {
62
              $in_ns = 0;
63
          }
64
          if ($in_ns) {
65
              $line =~ s/^\s+|\s+$//g;
66
              push @ns, uc $line;
67
          }
68
      }
69
      @ns = sort @ns;
70
  }
71
 
72
  return @ns;
73
}
74