IP Prefixes are allocated ..
Florian Weimer
fw at deneb.enyo.de
Mon Nov 28 07:20:29 UTC 2005
* Christopher L. Morrow:
> he might be satisfied with:
>
> mail.pch.net. 86400 IN A 206.220.231.1
>
> :~> host -W 6 -R 10 -t txt 1.231.220.206.asn.routeviews.org
> 1.231.220.206.asn.routeviews.org text "3856" "206.220.228.0" "22"
>
> which is AS 3856 routing 206.220.228.0/22 ... which contains the /32
> above.
asn.routeviews.org doesn't do longest-prefix matching, so you need a
short Perl script to get the correct ASN, attached below. However,
this is a bit slow thanks to the overhead of loading Net::DNS, see
<http://www.enyo.de/fw/notes/perl-probleme.html> (German).
#!/usr/bin/perl
use warnings;
use strict;
use Net::DNS;
if (@ARGV != 1 && $ARGV[0] !~ /^\d+\.\d+\.\d+\.\d+$/) {
print STDERR "usage: ip2asn A.B.C.D\n";
exit 1;
}
my $suffix = 'asn.routeviews.org';
my $name = join '.', (reverse split /\./, $ARGV[0]), $suffix;
my $res = Net::DNS::Resolver->new;
my $packet = $res->query($name, 'TXT');
my @txt;
@txt = $packet->answer if $packet;
my ($longest_net, $longest_length, $longest_asn);
for my $rr (@txt) {
my ($asn, $net, $length) = $rr->char_str_list;
if ((! defined $longest_length) || $length > $longest_length) {
$longest_net = $net;
$longest_length = $length;
$longest_asn = $asn;
}
}
if (defined $longest_asn && $longest_asn < 64511) {
print "$longest_asn\n";
} else {
print "0\n";
}
More information about the NANOG
mailing list