#!/usr/local/bin/perl
# -*- perl -*-

# Copyright (c) 1999 by Jeff Weisberg
# Author: Jeff Weisberg <jaw @ tcp4me.com>
# Function: get bgp table info

# usage:
# lookbgp ASN

require "open2.pl";


$asn = $ARGV[0];

# I'm lazy today, punt to netcat...
$pid = open2(R, W, "netcat route-server.cerf.net 23");

print W <<E;
term length 0
sho ip bgp reg _${asn}_
exit
E
    ;

close W;

while( <R> ){
    chop;
    $x = $_;

    if( /^\*. ?\d/ ){

	# print;print "\n";
	
	# by asn
	s/.*\s$asn\s*//o;
	s/^i/$asn I/;
	if( /^\d/ ){
	    split;
	    # print; print "\n";
	    # push @cust, $_[0];
	    $cust{ $_[0] } ++;
	}
	
	# prefix len
	$_ = $x;
	
	s/^\D*//;
	s/\s.*$//;

	($r, $l) = m%(.*)/(.*)%;
	if( !$l ){
	    $r = $_;
	    $a = int($r);
	    $l = 8;
	    $l = 16 if $a >= 128;
	    $l = 24 if $a >= 192;
	}
	# print "$r / $l\n";

	$n = 1 << ( 24 - $l );
	$nets += $n;
    }
}

foreach $a (sort keys %cust){
    $aa ++;
    $t += $cust{ $a };
    print "$a\t$cust{$a}\n";
}

print "Custs: $aa\n";
print "Routes: $t\n";
print "/24s: $nets\n";




    
