-
Notifications
You must be signed in to change notification settings - Fork 1
/
domain_check
executable file
·173 lines (155 loc) · 5.48 KB
/
domain_check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/local/cpanel/3rdparty/bin/perl
# CX-1105 - Request from Kyle P. domain_check
use strict;
use LWP::UserAgent;
use NetAddr::IP;
use Cpanel::Validate::IP();
use Getopt::Long;
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;
my $username;
my $help;
# Get servers IP addresses
my @ips=qx[ whmapi1 listips | grep 'public_ip:' | awk '{print $2}' ];
GetOptions(
'username=s' => \$username,
'help' => \$help,
);
if ( $help ) {
usage();
exit;
}
my @domains=@ARGV;
if ( !@domains ) {
# Open up /etc/userdomains and get all domains.
open( my $fh, '<', '/etc/userdomains' ) or return 0;
while (<$fh>) {
my ( $domain, $user ) = (split( /\: /, $_ ));
next if ( $domain eq '*' );
chomp( $user );
chomp( $domain );
if ( $username ) {
push @domains, $domain if ( $username eq $user );
}
else {
push @domains, $domain;
}
}
}
foreach my $domain(@domains) {
my $exists = qx[ grep $domain '/etc/userdomains' ];
print "$domain is not on this server\n" unless ( $exists );
exit unless ( $exists );
my ($username)=(split(/\s+/,qx[ whmapi1 getdomainowner domain=$domain | grep 'user:' ] ))[2];
chomp( $username );
my $domain_type = get_domain_type( $domain );
print CYAN "Domain: $domain " . WHITE "[ " . YELLOW $domain_type . WHITE " ]" . CYAN " - " . WHITE "( " . BOLD BLUE . $username . WHITE " )\n";
check_for_dns_error( $domain );
get_a_record( $domain );
get_mx_record ( $domain );
get_nameservers( $domain );
}
sub get_nameservers {
my $tcDomain = shift;
my @nameservers = qx[ dig +short $tcDomain NS ];
for my $ns(@nameservers) {
chomp( $ns );
print WHITE "\t\\_ Name Server: $ns\n";
}
print "=" x 80;
print "\n";
}
sub get_a_record {
my $tcDomain=shift;
my @arecords = qx[ dig +short $tcDomain A ];
for my $arecord(@arecords ) {
chomp( $arecord );
my $exists_here = check_if_ip_on_server( $arecord ) if ( $arecord );
if ( $arecord =~ m/[0-9\.]/ ) {
print YELLOW "\t\\_ A Record: $arecord - $exists_here\n" unless( $arecord =~ m/[a-z]/ );
}
if ( $arecord =~ m/[a-z].*/ ) {
print YELLOW "\t\\_ CNAME Record: $arecord - $exists_here\n" unless( $arecord =~ m/[0-9]/ );
}
print RED "\t\\_ No A record found\n" unless( $arecord );
print "=" x 80 unless( $arecord );
print "\n" unless( $arecord );
}
}
sub get_mx_record {
my $tcDomain=shift;
my @mxrecords = qx[ dig +short $tcDomain MX ];
for my $mx(@mxrecords) {
chomp($mx);
my ( $priority, $mxrec ) = ( split( /\s+/, $mx ));
return unless( $mxrec );
my $exists_here = check_if_ip_on_server( $mxrec ) if ( $mxrec );
# Check if $mxrec is a valid IP
my $isValidIP = (Cpanel::Validate::IP::is_valid_ip($mxrec)) ? 1 : 0;
unless( $isValidIP ) {
# Get A record of $mxrec
print BOLD BLUE "\t\\_ MX Record: $mxrec\n";
my @arecords = qx[ dig +short $mxrec A ];
for my $arecord(@arecords ) {
chomp( $arecord );
my $exists_here = check_if_ip_on_server( $arecord ) if ( $arecord );
if ( $arecord =~ m/[0-9\.]/ ) {
print YELLOW "\t\t\\_ A Record: $arecord - $exists_here\n" unless( $arecord =~ m/[a-z]/ );
}
if ( $arecord =~ m/[a-z].*/ ) {
print YELLOW "\t\t\\_ CNAME Record: $arecord - $exists_here\n" unless( $arecord =~ m/[0-9]/ );
}
print RED "\t\t\\_ No A record found\n" unless( $arecord );
}
}
}
}
sub check_if_ip_on_server {
my $tcArecord = shift;
my $isCloudFlare = check_cloudflare_ips($tcArecord);
if ( $isCloudFlare ) {
return BOLD YELLOW " [ CloudFlare IP ]" if ( $isCloudFlare );
}
my $exists = ( grep { /$tcArecord/ } @ips );
return BOLD GREEN " [ HOSTED HERE ]" if ($exists);
return MAGENTA " [ HOSTED ELSEWHERE ]";
}
sub check_for_dns_error {
my $tcDomain = shift;
my $geterror = qx[ dig $tcDomain +noall +comments +noedns | grep 'status:' | cut -d ' ' -f6 | tr -d , ];
chomp( $geterror );
return if ( $geterror eq 'NOERROR' );
print RED "DNS Error: $geterror\n";;
print "=" x 80;
print "\n";
}
sub check_cloudflare_ips {
my $chkIP = shift;
chomp($chkIP);
my $url = URI->new( 'https://www.cloudflare.com/ips-v4' );
my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } );
$ua->agent("");
my $res = $ua->get($url);
my $cf_subnets = $res->decoded_content;
my @cf_subnets = split /\n/, $cf_subnets;
foreach my $cf_subnet (@cf_subnets) {
chomp($cf_subnet);
my $network = NetAddr::IP->new($cf_subnet);
my $ip = NetAddr::IP->new($chkIP);
if ( $ip->within($network) ) {
return 1;
}
}
}
sub get_domain_type {
my $tcDomain = shift;
my ($domain_type)=(split(/\s+/,qx[ whmapi1 get_domain_info | grep -A1 $tcDomain | grep 'domain_type' ] ))[2];
chomp($domain_type);
return $domain_type;
}
sub usage {
print "domain_check -> runs for all domains on the server.\n";
print "domain_check -- username $username -> runs all domains for $username on the server.\n";
print "domain_check [domain1.tld] [domain2.tld] [domain3.tld] -> runs for just those domains.\n";
print "domain_check --help -> This.\n";
}