Team-Cymru have a incredible service called IP to ASN which one enters an IP address and receives the AS number info.

I have implemented a Perl module (teamasn) to retrieve such info  in a very simple way. ‘teamasn’ module depends on ‘Net::DNS::Dig’ to sent a DNS query to Team-Cymru’s service.  My module is a parser to retrieve the ‘ANSWER’ section. Yes, Perl can be simple :)

For example, to find the AS number from IP 216.90.108.2 using DIG, type in the terminal:

 $ dig +short 2.108.90.216.origin.asn.cymru.com TXT
 "23028 | 216.90.108.0/24 | US | arin | 1998-09-25"

How to use it in Perl:

teamasn::get_dns_answer('216.90.108.2');

‘teamasn’ module returns an array composed by the AS number ( 23028), Network Class (216.90.108.0/24), Country Code (US), Registry (arin), and Allocation Date (1998-09-25). During my tests I have seen some IPs which ‘Allocation Date’ is absent, therefore one needs to check the array size to know how much info ‘teamasn’ got.

Here it goes a sample code:

#!/usr/bin/perl
# @kaiux
use teamasn;
use strict;
use warnings;
use v5.14;

# IPV4 only :/
my @ip_list = qw(200.242.79.172 200.141.79.11 199.16.158.104 94.100.180.202 217.69.139.200 192.168.0.1);

foreach my $ip (@ip_list)
{
	my @response = teamasn::get_dns_answer($ip);
	if ( $response[0] == -1 )
	{
		say "There is no info for: ", $ip;
	}
	else
	{
		say "AS: ", $response[0];
		say "NC: ", $response[1];
		say "CC: ", $response[2];
		say "RI: ", $response[3];
		say "DT: ", $response[4];
	}
}

One can find the ‘teamasn’ source code at: https://bitbucket.org/kaiorafael/dns_tools/

I will not push ‘teamasn’  to CPAN because it is very premature code and I need extra time to add new features such as:

get_asn: retrieve the AS number only
get_nwc: retrieve the network class only
get…etc…
IPV6….

There are other modules to request AS info such as Net::Whois, however it is HTTP-based and for my needs DNS is better because it is very fast.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.