Re: [c-nsp] Available Ethernet switch ports

Aldo Valente aldo.valente at gmx.de
Mon Jul 11 06:20:07 EDT 2005


> I am looking for an easier way of working out which ethernet ports on
> any given Cisco switch are either available or have never been
> up/connected, rather than tracing cabling back to see if it is patched
> and in use.

Besides netdisco (which i love) i monitor every switchport with MRTG.
I use an ugly perlscript which parses "sh int status".  This will 
show notconnected Ports.  From the HTML Output is a link to the MRTG Data.

When i am looking for a free Port, i use the Webpage to search for
notconnected Ports.  The MRTG Data tells me the last time the given 
Switchport was used.

Ugly perl comes here.  


Aldo
#!/usr/bin/perl
#
# Aldo Valente 2004 -- switchport
#
# copyright: GPL
#
# this reads "sh int status"  from IOS Switches (and perhaps even "sh port
status"
# form CatOS Switches) and generates some HTML which embeds MRTG Graphs.
#
# This just works for me.  If you love perl, this could hurt your eyes.
#  
# i use clogin (from rancid) to get the output of "sh int status in a
# shellscript, running from crontab.
#
# for i in $IOSSWITCH
# do
#   clogin -c "sh int status" $i > $i 
#   switchport $i > $i.html
# done

$columns=6;

sub tablerow {
  #for $i ( 1 .. 2) {
  my $i = $_[2];
  print "<tr>";
  for $j ( $_[0] ..  $_[1]  ) {
    if ($port[$i][$j]{'vlan'} =~ /\d+/i) {
     $VLAN="VLAN <b>$port[$i][$j]{'vlan'}</b>";
    } elsif ($port[$i][$j]{'vlan'} =~ /trunk/i) {
      $VLAN="<i>Trunk</i>";
    }
    print "<th><nobr><font size=\"-1\"> $j
$port[$i][$j]{'speed'}/$port[$i][$j]{'duplex'} $VLAN</font></nobr>" 
      if $port[$i][$j]{'modul'};
  }
  print "</tr>";
  print "<tr>\n";
  for $j ( $_[0] .. $_[1]) {
    # sw01_fa0_11-day.png
    $bgpic = "/cgi-bin/rrd.cgi/$name/". "$name" . "_" .
$port[$i][$j]{'modul'} . "_" . "$j/${name}_" 
             . lc($port[$i][$j]{'modul'}) . "_${j}-day.png";
    $bgi  = "<img src=\"$bgpic\" height=\"80\" width=\"200\">" ;
    $link = "/cgi-bin/rrd.cgi/$name/". "$name" . "_" .
$port[$i][$j]{'modul'} . "_" . "$j/";
    $alink = "<a href=\"$link\" target=\"_new\">$bgi</a>\n";

    if ( $port[$i][$j]{'status'} =~ /notconnect/ ) {
      print "<td valign=\"top\" bgcolor=\"#FFA0A0\">";
    } elsif ( $port[$i][$j]{'status'} =~ /monitor/ ) {
      print "<td valign=\"top\" bgcolor=\"#AOA0FF\">";
    } elsif ($port[$i][$j]{'speed'} =~ /1000/) {
      print "<td valign=\"top\" bgcolor=\"#FFFF00\">";
    } elsif ($port[$i][$j]{'speed'} =~ /100/) {
      print "<td valign=\"top\" bgcolor=\"#FFFF77\">";
    } elsif ($port[$i][$j]{'speed'} =~ /16/) {
      print "<td valign=\"top\" bgcolor=\"#FFFF00\">";
    } else {
      print "<td valign=\"top\" bgcolor=\"#FFFFFF\">";
    }
    unless ( $port[$i][$j]{'status'}) {
      print "</td>\n";
      next;
    }
    print "$port[$i][$j]{'name'}";
    if ($port[$i][$j]{'status'} !~ /connec/) {
      print "<b><font color=\"#FF00FF\">$port[$i][$j]{'status'}</font></b>";
    }
    print "<br>$alink</td>";
  }
  print "</tr>\n";
}

## Main

$name =  $ARGV[0];

 while (<>)  {  
   chomp;

next unless /^\s*\w{0,2}\d{1,2}\/\d+/;
#print STDERR $_ , "\n";
# Name and Type can contain Spaces, so we first split on Status

($f1, $status, $f3) = split
/(err-disabled|notconnect|disabled|monitor|connected)/,$_;

($modul, @f4) = split /\//, $f1;

$modul =~ s/\s//g;
$f4 = "@f4";
($port, @name ) = split /\s+/,$f4;
unless ( $modul eq $oldmodul ) {

  $oldmodul = $modul;
  $modul =~ /\w{1,2}(\d{1,2})/;
  $mod++;
}

$port[$mod][$port]{'modul'} =  $modul;
$port[$mod][$port]{'name'} = "@name";
$port[$mod][$port]{'status'} = $status;
@row =  split /\s+/, $f3;
shift @row; # junk
$port[$mod][$port]{'vlan'} = shift @row;
# 'Level' seems only to exist on Cat5000 and its value is always 'normal'
$row[0] eq 'normal' && shift @row;
$port[$mod][$port]{'duplex'} = shift @row;
$port[$mod][$port]{'speed'} = shift @row;
$port[$mod][$port]{'type'} = "@row";
$port[$mod][0]   = $port; 

}
# sw01_fa0_11-day.png
print '<html><head><meta=http-equiv=\"expires\" 
content=\"0\"><title>Portbelegung</title></head><body>';
print "<h2> $name -- Aktualisiert:", `date`,"</h2>";
print "Die Links über den Ports führen zu den MRTG Auslastungsdaten (falls
vorhanden).<br>";
print "<a href=\"/cgi-bin/rrd.cgi/$name/\">Alle Interfaces direkt im
MRTG</a><br>";
print "Rot:  notconnected, Blau: Monitor, Gelb:  Speed 100|16, Sonst:
Weiß<p>";
print '<table border="1" ><tr>';

print "</tr>\n";

for $l ( 1 .. $mod ) {
  $to =  $port[$l][0] / $columns || 1 ;
  $to++ if (int $to < $to);
  $to = int $to;
    print "<tr><td colspan=\"$columns\"
height=\"60\"bgcolor=\"99cccc\">Modul $port[$l][1]{'modul'}
 -- $port[$l][1]{'type'}</td></tr>";
  for $k ( 1 .. $to) {
        tablerow(($k-1)*$columns+1,$k*$columns,$l);
      }

}

print "</table></body></html>";



-- 
Weitersagen: GMX DSL-Flatrates mit Tempo-Garantie!
Ab 4,99 Euro/Monat: http://www.gmx.net/de/go/dsl


More information about the cisco-nsp mailing list