usr bin perl use strict my peers my mxs sub traverse my host shift my

 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
#!/usr/bin/perl
use strict;
my @peers;
my @mxs;
sub traverse {
my $host = shift;
my @nameservers;
open(NS, "host -t ns $host |") or die "Cannot get list of nameservers";
while (<NS>) {
if (/name server ([\d_-\w\.]+)/) {
push @nameservers, $1;
}
}
foreach(@nameservers) { # try AXFR
open(HOSTS, "host -l $host $_ |") or print "Cannot list domain via $_\n";
while(<HOSTS>) {
if (/([\d_-\w\.]+) has address ([\d\.]+)/) {
push @peers, $2;
# print "$1\n";
# can get it recursive here
}
}
}
open(MX, "host -t mx $host |"); # try MX
while(<MX>) {
if (/mail is handled by \d+ ([\d_-\w\.]+)/) {
push @mxs, $1;
}
}
}
sub check_availability {
my $host = shift;
print $host;
`ping -W1 -qc2 $host`;
print " (alive)" if (0 == $?);
}
my $host = shift @ARGV;
traverse $host;
my %mx_ips; # hash of arrays: {mx -> ip0, ip1, ...}
foreach(@mxs) {
my $mx = $_;
open(IP, "host -t a $mx |");
while(<IP>) {
if (/has address ([\d\.]+)/) {
push @{$mx_ips{$mx}}, $1;
@peers = grep {!/$1/} @peers; # filter it out from peers
}
}
}
print "MXs: ";
foreach(keys %mx_ips) {
print "\t$_: ";
foreach(@{$mx_ips{$_}}) {
check_availability $_;
}
print "\n";
}
my %seen; my @uniq_peers = grep !$seen{$_}++, @peers; # make it uniq
print "Other:\n" if $#uniq_peers > 0;
foreach(@uniq_peers) {
check_availability $_;
print "\n"
}