sabato, Dicembre 21, 2024

Ribstat: Routing Table Statistics in Perl

Gianluca Lini
Gianluca Linihttp://www.gianlucalini.it
Technology Enthusiast. I'm a System Engineer and sometimes an independent Security Researcher. IEEE member.

Un semplice script in Perl che prende in input da un file ip-routes la tabella di routing ottenuta con sh ip route e fornisce in output alcune statistiche.
Eccovi un esempio di output:

./ribstats.pl
------------------------------------------
Routing Table Statistics -- Size: 15 MB
------------------------------------------


Directly Connected Routes :43
Static Routes :4
BGP Routes :203573
EIGRP Routes :
EIGRP Internal Routes :17
EIGRP External Routes :
OSPF Routes :27418
OSPF Inter Area Route :1096 32: 532 30: 249
OSPF NSSA external type 1 Routes :
OSPF NSSA external type 2 Routes :
OSPF external type 1 Routes :25165 32: 8898 30: 7470
OSPF external type 2 Routes :1157 32: 614 30: 110
IS-IS Routes :
L1 :
L2 :


------------------------------------------
Prefix Report
------------------------------------------


OSPF Prefix Distribution
32 :10044 %:36.633
30 :7829 %:28.554
29 :7611 %:27.759

Segue il codice Perl di ribstat.pl:

#!/usr/bin/perl
use File::stat;
#--------------------------------------------------------------------#
# Description: This is a script that analyses the output of
# show ip route command and then dumps the results to standard output.
# If you have any comments and/or suggestions,pleas email them to me.
#--------------------------------------------------------------------#
# Ver	Subver		    Date                Email
# 01    05                  1/12/06             [email protected]
#--------------------------------------------------------------------#
#
open(RIB,"ip-routes") || die "Can't open ip-routesn";
#
# RIB Processing
#
while (<RIB>){
#$_=substr($_,0,4);
#
if	($_ =~ /^C.+d/)
	{ ++$direct; }
elsif
	($_ =~ /^S.+d/)
	{ ++$static; }
elsif
	($_ =~ /^D.EX.+d/)
	{ ++$exteigrp; }
elsif
	($_ =~ /^D.+d/)
	{	 ++$inteigrp; }
elsif
	($_ =~ /^R.+d/)
	{	 ++$rip; }
elsif
	($_ =~ /^B.+d/)
	{	 ++$bgp; }
elsif
	($_ =~ /^O.IA.+d/)
	{	 ++$ospfia;
	         ++$ospf;
              if ($_ =~ /^O.IA.+/32/)
	         { ++$ospfia32;
                   ++$totospf32;
                 }
              if ($_ =~ /^O.IA.+/30/)
	         { ++$ospfia30;
                   ++$totospf30;
                 }
             if ($_ =~ /^O.IA.+/29/)
	         { ++$ospfia29;
                   ++$totospf29;
                 }
        }

elsif
	($_ =~ /^O.N1.+d/)
	{	 ++$ospfn1;
                 ++$ospf;
                 if ($_ =~ /^O.N1.+/32/)
	         { ++$ospfn132;
                   ++$totospf32;
                 }
            if ($_ =~ /^O.N1.+/30/)
	         { ++$ospfn130;
                   ++$totospf30;
                 }
            if ($_ =~ /^O.N1.+/29/)
	         { ++$ospfn129;
                   ++$totospf29;
                 }
        }
elsif
	($_ =~ /^O.N2.+d/)
	{	 ++$ospfn2;
                 ++$ospf;
                 if ($_ =~ /^O.N2.+/32/)
	         { ++$ospfn232;
                   ++$totospf32;
                 }
            if ($_ =~ /^O.N2.+/30/)
	         { ++$ospfn230;
                   ++$totospf30;
                 }
            if ($_ =~ /^O.N2.+/29/)
	         { ++$ospfn229;
                   ++$totospf29;
                 }
        }
elsif
	($_ =~ /^O.E1.+d/)
	{	 ++$ospfe1;
	         ++$ospf;
            if ($_ =~ /^O.E1.+/32/)
	         { ++$ospfe132;
                   ++$totospf32;
                 }
            if ($_ =~ /^O.E1.+/30/)
	         { ++$ospfe130;
                   ++$totospf30;
                 }
            if ($_ =~ /^O.E1.+/30/)
	         { ++$ospfe129;
                   ++$totospf29;
                 }
        }
elsif
	($_ =~ /^O.E2.+d/)
	{	 ++$ospfe2;
	         ++$ospf;
         if ($_ =~ /^O.E2.+/32/)
	         { ++$ospfe232;
                   ++$totospf32;
                 }
         if ($_ =~ /^O.E2.+/30/)
	         { ++$ospfe230;
                  ++$totospf30;
                 }
         if ($_ =~ /^O.E2.+/30/)
	         { ++$ospfe229;
                   ++$totospf29;
                 }
        }
elsif
	($_ =~ /^i.L1.+d/)
	{ ++$isis;
    ++$isisl1;
   }
elsif
	($_ =~ /^i.L2.+d/)
	{ ++$isis;
    ++$isisl2;
  }
}
#
#   	print the  results
#
my $filesize = stat("ip-routes")->size;
print "n------------------------------------------n";
print " Routing Table Statistics -- Size: ",int($filesize/(1024*1024))," MBn";
print "------------------------------------------nn";
print "Directly Connected Routes        :",$direct,"n";
print "Static Routes                    :",$static,"n";
print "BGP Routes                       :",$bgp,"n";
print "EIGRP Routes                     :",$eigrp,"n";
print "EIGRP Internal Routes            :",$inteigrp,"n";
print "EIGRP External Routes            :",$exteigrp,"n";
print "OSPF Routes                      :",$ospf,"n";
print "OSPF Inter Area Route            :",$ospfia," \32: ",$ospfia32," \30: ",$ospfia30,"n";
print "OSPF NSSA external type 1 Routes :",$ospfn1,"n";
print "OSPF NSSA external type 2 Routes :",$ospfn2,"n";
print "OSPF external type 1 Routes      :",$ospfe1," \32: ",$ospfe132," \30: ",$ospfe130,"n";
print "OSPF external type 2 Routes      :",$ospfe2," \32: ",$ospfe232," \30: ",$ospfe230,"n";
print "IS-IS Routes                     :",$isis,"n";
print "L1                               :",$isisl1,"n";
print "L2                               :",$isisl2,"n";
print "n------------------------------------------n";
print " Prefix Reportn";
print "------------------------------------------nn";
print "OSPF Prefix Distribution                     n";
print "\32                              :",$totospf32,"t %:",sprintf("%.3f",$totospf32*100/$ospf),,"n";
print "\30                              :",$totospf30,"t %:",sprintf("%.3f",$totospf30*100/$ospf),"n";
print "\29                              :",$totospf29,"t %:",sprintf("%.3f",$totospf29*100/$ospf),"n";
close(RIB);
exit

Articoli correlati

Noleggia una Tesla per il tuo evento ICT!

Categorie