Home page logo
/

nanog logo nanog mailing list archives

Re: scripts to map IP to AS?
From: Simon Lyall <simon.lyall () ihug co nz>
Date: Fri, 21 Feb 2003 10:15:33 +1300 (NZDT)


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;




  By Date           By Thread  

Current thread:
[ Nmap | Sec Tools | Mailing Lists | Site News | About/Contact | Advertising | Privacy ]