[86987] in North American Network Operators' Group
Re: IP Prefixes are allocated ..
daemon@ATHENA.MIT.EDU (Florian Weimer)
Mon Nov 28 02:21:06 2005
From: Florian Weimer <fw@deneb.enyo.de>
To: "Christopher L. Morrow" <christopher.morrow@mci.com>
Cc: Bill Woodcock <woody@pch.net>, Glen Kent <glen.kent@gmail.com>,
NANOG list <nanog@merit.edu>
Date: Mon, 28 Nov 2005 08:20:29 +0100
In-Reply-To: <Pine.GSO.4.58.0511280644000.20032@marvin.argfrp.us.uu.net>
(Christopher L. Morrow's message of "Mon, 28 Nov 2005 06:46:48 +0000
(GMT)")
Errors-To: owner-nanog@merit.edu
* 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";
}