[55989] in North American Network Operators' Group

home help back first fref pref prev next nref lref last post

Re: scripts to map IP to AS?

daemon@ATHENA.MIT.EDU (Simon Lyall)
Thu Feb 20 16:16:44 2003

Date: Fri, 21 Feb 2003 10:15:33 +1300 (NZDT)
From: Simon Lyall <simon.lyall@ihug.co.nz>
To: <nanog@merit.edu>
In-Reply-To: <3E54D37D.4617B03D@greendragon.com>
Errors-To: owner-nanog-outgoing@merit.edu


On Thu, 20 Feb 2003, William Allen Simpson wrote:
> Anybody have a pointer to scripts to map IP to AS?

This little script works fairly well. Just feed it a file with the each
network on a seperate line. Obviously don't overload the route servers by
running it too often.

-- 
Simon Lyall.                |  Newsmaster  | Work: simon.lyall@ihug.co.nz
Senior Network/System Admin |  Postmaster  | Home: simon@darkmere.gen.nz
Ihug Ltd, Auckland, NZ      | Asst Doorman | Web: http://www.darkmere.gen.nz


#!/usr/bin/perl -U

$host = "route-server.cerf.net" ;

use IO::Socket;
open(FILE,"$ARGV[0]");
@length = <FILE>;
chop(@length);


# Open connection

$remote = IO::Socket::INET->new(
	Proto => "tcp",
	PeerAddr => $host,
	PeerPort => 23,
	);
	unless ($remote) { die "cannot connect to remote host $host" }
 	$remote->autoflush(1);

# send "sh ip bgp ip" for each ip

while($count<@length) {
	# set AS number to zero
	$asnum = 0;
	#print "$length[$count] \n";

	# Chop of the /24  /16 bit etc
	($network) = split(/\//, $length[$count]);
	print $remote "show ip bgp $network \n \n";
	while (defined ($line = <$remote> )) {
		# break when we have out first result
     		last if ( $line=~/localpref/ );
		last if ( $line=~/not in table/ );
		# Checked for Cerf's AS number
		# print "line is $line \n";
	        if ($line=~/17233/) {
			# Get rid of bogus stuff after a ","
			# print "$line \n";
			($line) = split(/,/, $line);
			# print "$line \n";
			# Grab last number in the list
			@Fld = split(' ', $line);
        		$asnum=$Fld[$#Fld];
			}
		}
#	printf "$length[$count]\tAS$asnum.\n";
	printf "AS$asnum.\t$length[$count]\n";

	open(OUTPUT,">>$ARGV[1]");
	printf OUTPUT "AS$asnum.\t$length[$count]\n";
	close(OUTPUT);

	sleep 5;
	$count++ ;
	# following clears any extra lines returned by the command.
	while ( defined ($line = <$remote> )) {
		last if ( $line=~/route-server/ );
		}
	}

# Close conenction.

close $remote;




home help back first fref pref prev next nref lref last post