Pages

7/14/2009

LOOKING GLASS
Example script to check several telnet route servers and create html file of AS-PATH


use Net::Telnet ();

my $outdir = "\\inetpub\\wwwroot\\look\\data";
my $idxdir = "\\inetpub\\wwwroot\\look";
my $url = "data";
mkdir ($idxdir);
mkdir ($outdir);
#list of public routers accepting telnet with no logon
my @list2 = (
"route-views.optus.net.au" ,
"route-server.videotron.net" ,
"route-server.ipilink.net" ,
"route-server.host.net" ,
"public-route-server.is.co.za" ,
"route-server-east.gt.ca" ,
"route-server-west.gt.ca" ,
"route-server.gt.ca" ,
"route-views.on.bb.telus.com" ,
"route-views.ab.bb.telus.com" ,
"routeserver.sunrise.ch" ,
"route-server.belwue.de" ,
"route-server.ip.tiscali.net" ,
"route-server.eu.gblx.net" ,
"route-server.cerf.net" ,
"route-server.gblx.net" ,
"route-server.host.net" ,
"route-server.he.net" ,
"route-server.twtelecom.net" ,
"public-route-server.is.co.za" ,
"route-server.central.allstream.com" ,
"route-server.east.allstream.com" ,
"route-server.west.allstream.com" ,
"route-server.vtl.net" ,
"route-server.eastlink.ca" ,
);

#date
my ($sec,$min,$hour,$mday,$mon,$year,
$wday,$yday,$isdst) = localtime time;
$year = $year+1900;
$mon = $mon+1;
$mon = sprintf("%02d", $mon);
$mday = sprintf("%02d", $mday);
$hour = sprintf("%02d", $hour);
$min = sprintf("%02d", $min);
my $date = "$mon\-$mday\-$year $hour:$min";

#Build report

my $file = "$outdir\\$year\-$mon\-$mday\-$hour$min.html";
open FIL , ">$file";

print FIL <<ENDHEADER;
<HTML>
<HEAD>
<TITLE>Looking Glass Results</TITLE>
<META HTTP-EQUIV="Refresh" CONTENT="300">
<META HTTP-EQUIV="Cache-Control" content="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="Mon, 16 May 2005 13:59:00 GMT">
<style type="text/css">
H1 {font-family:serif; border-width: 0; border-bottom: solid; text-align: left}
H3 {font-family:serif; padding-top:0px;color:#000000;padding-bottom:0px;margin-bottom:0px;}
body {font-family:monospace;font-size:.75em;margin:3%;padding:0px;background:#aabbcc;color:#000000;}
</style>
</HEAD>

<BODY bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000" alink="#000000">

<H1>AS-PATH to 4.4.4.0/24</H1>
<H4>$date</H4>
<HR>
ENDHEADER
;
#telnet to each router and query for info
foreach $router (@list2) {
print ".";
my (@result, $t);
print FIL "<H2>$router</H2>\n";
$t = new Net::Telnet (Timeout => 20, Errmode => "return");
$t->open("$router");

## Wait for prompt and enter command.
$t->waitfor('/>/') or print "X";
$t->print("show ip bgp 4.4.4.0/24");
($result) = $t->waitfor('/>/');

#Filter results for lines containing (AS) 32166
@result = split(/\n/, $result);
my $prev = "prior line\n";
print ".";
foreach $line (@result) {
if ($line eq $prev) {
next
};
if ($line =~ /32166/) {
push (@output, "$line<br>\n");
$prev = $line;
}
print ".";
}

print FIL "<p>@output</p>\n";
print FIL "<HR>\n";
@output="";
$result="";
@result="";
print ".";
}

print "\n";
print FIL <<ENDTAIL;
</TABLE>

</BODY>
</HTML>

ENDTAIL
;

close FIL;

#Build index of reports

open IDX , ">$idxdir\\look.html";

print IDX <<IDXHEADER;
<HTML>
<HEAD>
<TITLE>Looking Glass Results</TITLE>
<META HTTP-EQUIV="Refresh" CONTENT="300">
<META HTTP-EQUIV="Cache-Control" content="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="Mon, 16 May 2005 13:59:00 GMT">
<style type="text/css">
H1 {font-family:serif; border-width: 0; border-bottom: solid; text-align: left}
H3 {font-family:serif; padding-top:0px;color:#000000;padding-bottom:0px;margin-bottom:0px;}
body {font-family:monospace;font-size:.75em;margin:3%;padding:0px;background:#aabbcc;color:#000000;}
</style>
</HEAD>

<BODY bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000" alink="#000000">

<H1>INDEX</H1>
<H3>AS-PATH to 4.4.4.0/24</H3><BR>
IDXHEADER
;

my ($path, $file, @files, $file_full_path);
opendir (SOURCE, $outdir) or die "Cannot open the source folder for reading: $!\n";
my @sorted_files =
map $_->[1],
sort { $a->[0] <=> $b->[0] }
map -f "$outdir/$_" ? [ ( stat _ )[9], $_ ] : (),
readdir SOURCE;


closedir (SOURCE);
for (@sorted_files){
$file_full_path = "$url/$_";
print IDX qq(<a href="$file_full_path" target="_blank">$_</a>     \n);
} #end for






print IDX <<IDXTAIL;
</TABLE>

</BODY>
</HTML>

IDXTAIL
;

close IDX

No comments: