-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathip2asn.pl
executable file
·66 lines (45 loc) · 1.18 KB
/
ip2asn.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
64
65
66
#!/usr/bin/perl -W
$|=1;
#################################################################
# Author: vvuksan
#################################################################
use Data::Dumper;
use Socket;
use lib "./";
require 'config.pm';
require 'tools.pm';
# Stick ASN mapping into a hash
open(ASN_LIST, "< $CFG{'asn_mapping_file'}");
my %asn;
while(<ASN_LIST>)
{
chop;
my ($as, $country, $description) = split /,/;
$asn{"$as"} = "$country = $description";
}
close(ASN_LIST);
my %longip2asn;
open(IP2LONG, "< $CFG{'long_ip_to_asn_file'}");
while(<IP2LONG>)
{
chop;
my ($longip, $as) = split /,/;
$longip2asn{$longip} = $as;
}
close(IP2LONG);
my $ip = $ARGV[0];
# We really only care for /24
my ( $part1, $part2, $part3, undef ) = split /\./, $ip;
my $slash24 = "${part1}.${part2}.${part3}.0";
my $ip_value = ip2long($slash24);
for ($i=8; $i<32; $i++) {
$ip2 = ($ip_value >> $i) << $i;
if ( $longip2asn{$ip2} ) {
$key = "AS" . $longip2asn{$ip2};
#$ip2asn{$slash24} = $asn;
last;
}
}
$asn{$key} = find_asn_org_name($key ,$CFG{'asn_mapping_file'});
printf "IP=%s %25s %s\n", $ip, "http://bgp.he.net/$key", $asn{$key};
exit 0;