-
Notifications
You must be signed in to change notification settings - Fork 4
/
bulk-example.pl
executable file
·64 lines (52 loc) · 1.03 KB
/
bulk-example.pl
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
#!/usr/bin/perl -w
#
# Otmar Lendl <[email protected]> 2009/09/02
#
#
use strict;
use Getopt::Long;
use DBI;
use Net::CIDR ':all';
use Data::Dumper;
use Net::Patricia;
my $cidrtree = new Net::Patricia;
my $debug = 0;
my $sep=';';
&GetOptions ( "d", \$debug,
"s=s", \$sep,
);
open(O, "whois -h qualle.cert.at v4table block origin |") or die "opening Qualle";
print STDERR "Loading Routing table : " if ($debug);
my $count = 0;
while($_ = <O>) {
if (/^v4table ([0-9.\/]+)\s(\d+)/) {
$cidrtree->add_string($1, $2);
if ($debug) {
print STDERR "." unless ($count++ % 1000)
}
}
}
close(O);
print STDERR "\nLoaded $count prefixes\n" if ($debug);
$count = 0;
while(<>) {
s/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ip2origin($1)/ge;
print;
# print STDERR "." unless ($count++ % 1000);
}
sub ip2origin {
my $ip = $_[0];
my $str = "";
my $id;
#
# where is it in the radix tree?
#
eval {
my $origin = $cidrtree->match_string($ip);
if (defined($origin)) {
return($ip . $sep . $origin);
} else {
return("$ip (NO match)");
}
}
}