Catalyst 5505 Mac Address table on port (cisco snmp switch mac script)
Ключевые слова: cisco, snmp, switch, mac, script, (найти похожие документы)
Date: Mon, 15 Oct 2001 05:41:14 +0000 (UTC)
From: Serov Alexander <serov@micex.com>
Newsgroups: fido7.ru.cisco
Subject: Catalyst 5505 Mac Address table on port
Надеюсь модератор не обидится! В $comm пишем ro community а в списке $cat
перечисляем все свои каталисты и не только 5505.
На свежих версиях CatOS переменная $blackList не нужна.
#!/usr/local/bin/perl
## Author: Serov Alexander (serov@micex.com)
use SNMP;
$SNMP::use_enums = 1;
$SNMP::auto_init_mib = 0;
my $comm = 'public;-)';
my %snmphunter;
my %porttable;
my %blackList;
$vtpVlanName = '.1.3.6.1.4.1.9.9.46.1.3.1.1.4';
$dot1dTpFdbPort = '.1.3.6.1.2.1.17.4.3.1.2';
$portTableMIB = '.1.3.6.1.4.1.9.5.1.4.1.1.3';
$PortIfIndex = '.1.3.6.1.2.1.17.1.4.1.2';
$ifName = '.1.3.6.1.2.1.31.1.1.1.1';
$vlan2vlan = '.1.3.6.1.4.1.9.5.1.9.3.1.3.';
$qwery = new SNMP::Varbind([]);
$qwery1 = new SNMP::Varbind([]);
$qwery2 = new SNMP::Varbind([]);
open( SNMPHUNTER,"/home/gosha/snmphunter.dat");
while(<SNMPHUNTER>) {
chomp;
tr /[\t,' ']//d;
@tmp = split(/:/);
for $i (1..8) {
if ( $tmp[$i]==undef ) { $tmp[$i]=""; };
};
if ( $tmp[9]==undef ) { $tmp[9]=time(); };
@snmphunter{ $tmp[0] } = [ @tmp[1..9] ];
}
close SNMPHUNTER;
foreach $cat (
'10.0.0.1',
'10.0.0.2' ) {
delete @porttable{ keys %porttable };
delete $blackList{ keys %blackList };
$sess2 = new SNMP::Session(DestHost=>$cat,Community =>$comm);
$qwery2->[$SNMP::Varbind::tag_f] = $portTableMIB;
for ( $mib_v = $sess2->getnext($qwery2);
$qwery2->tag=~/$portTableMIB/ and not $sess2->{ErrorStr};
$mib_v = $sess2->getnext($qwery2)) {
@vn = split(/\./,$qwery2->[$SNMP::Varbind::tag_f]);
if ( $vn[14]==2 || $vn[14]==3 ) {
$porttable{$mib_v} = [$vn[14],$vn[15]];
};
if ( $vn[14]==4 ) {
if ( $cat ne '195.5.1.111' && $cat ne '195.5.1.112' ) {
$porttable{$mib_v} = [$vn[14],$vn[15]];
};
};
};
$sess1 = new SNMP::Session(DestHost=>$cat,Community =>$comm);
$qwery1->[$SNMP::Varbind::tag_f] = $dot1dTpFdbPort;
for ( $mib_v1 = $sess1->getnext($qwery1);
$qwery1->tag=~/$dot1dTpFdbPort/ and not $sess1->{ErrorStr};
$mib_v1 = $sess1->getnext($qwery1)) {
@vn = split(/\./,$qwery1->[$SNMP::Varbind::tag_f]);
$mac = sprintf("%.2x\.%.2x\.%.2x\.%.2x\.%.2x\.%.2x",
$vn[12],$vn[13],$vn[14],$vn[15],$vn[16],$vn[17] );
$blackList{$mac} = 1;
};
$sess = new SNMP::Session(DestHost=>$cat,Community =>$comm);
$qwery->[$SNMP::Varbind::tag_f] = $vtpVlanName;
for ( $mib_v = $sess->getnext($qwery);
$qwery->tag=~/$vtpVlanName/ and not $sess->{ErrorStr};
$mib_v = $sess->getnext($qwery)) {
@vn = split(/\./,$qwery->[$SNMP::Varbind::tag_f]);
$vlan = $vn[16];
$tmp_comm = sprintf("%s\@%d",$comm,$vlan);
$sess1 = new SNMP::Session(DestHost=>$cat,Community =>$tmp_comm);
$qwery1->[$SNMP::Varbind::tag_f] = $dot1dTpFdbPort;
for ( $mib_v1 = $sess1->getnext($qwery1);
$qwery1->tag=~/$dot1dTpFdbPort/ and not $sess1->{ErrorStr};
$mib_v1 = $sess1->getnext($qwery1)) {
@vn = split(/\./,$qwery1->[$SNMP::Varbind::tag_f]);
$mac = sprintf("%.2x\.%.2x\.%.2x\.%.2x\.%.2x\.%.2x",
$vn[12],$vn[13],$vn[14],$vn[15],$vn[16],$vn[17] );
if ( $porttable{$mib_v1}[0] ne undef && $blackList{$mac}!=1 ) {
$slot=$porttable{$mib_v1}[0];
$port=$porttable{$mib_v1}[1];
$sess2 = new SNMP::Session(DestHost=>$cat,Community =>$comm);
$port--;
$vlan = $sess2->getnext(["$vlan2vlan"."$slot"."."."$port", "" ]);
$port++;
$snmphunter{ $mac }[0] = $vlan;
$snmphunter{ $mac }[1] = $cat;
$snmphunter{ $mac }[2] = $slot;
$snmphunter{ $mac }[3] = $port;
$snmphunter{ $mac }[8] = time();
# print "$mac:$snmphunter{ $mac }[0]:$snmphunter{
$mac }[1]\/$snmphunter{ $mac }[2]\/$snmphunter{ $mac }[3]\n";
};
};
};
};
rename "/home/gosha/snmphunter.dat", "/home/gosha/snmphunter.dat.bak";
open( SNMPHUNTER,">/home/gosha/snmphunter.dat");
foreach $mac ( keys(%snmphunter) ) {
@tmp = @{ $snmphunter{ $mac }};
$tmp = join(":",($mac,@tmp));
print SNMPHUNTER $tmp,"\n";
print $tmp,"\n";
};
close SNMPHUNTER;
# print "ok";