Difference between revisions of "Net: : SNMP bulk request example"
From Teknologisk videncenter
m (New page: <source lang=perl> #!/usr/bin/perl #=============================================================================== # # FILE: snmpbulk.pl # # USAGE: ./snmpbulk.pl # # DES...) |
(No difference)
|
Revision as of 16:04, 10 November 2009
#!/usr/bin/perl
#===============================================================================
#
# FILE: snmpbulk.pl
#
# USAGE: ./snmpbulk.pl
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Henrik Thomsen (HeTh), <heth@eucmidt.dk>
# COMPANY: EUC MIDT, Viborg, Denmark
# VERSION: 1.0
# CREATED: 10/04/08 19:01:51 CEST
# REVISION: ---
#===============================================================================
# notes: SNMP-NET /usr/share/snmp/mibs
use strict;
use warnings;
use Net::FTP;
use Net::SNMP qw(:snmp);
use diagnostics;
my $MIBLIB = "$ENV{'HOME'}/snmp/mib";
my $COMPILELIB = "$ENV{'HOME'}/snmp/compiler";
my $SITECONFIG = "$ENV{'HOME'}/snmp/site.conf";
my @miblist;
my $ciscoMibList = ".1.3.6.1.2.1.1.9.1.3"; #Cisco MIB list
my ( $session,$error,$result);
#my @files =("IP-MIB","IF-MIB","IPMROUTE-MIB","IPMROUTE-STD-MIB","CISCO-IP-IF-MIB");
findCiscoDeviceMibs('192.168.22.1');
checkdepend('cisco',\@miblist);
############################################
# sub: findCiscoDeviceMibs
############################################
sub findCiscoDeviceMibs {
my $device = shift;
my $community = shift || 'public';
($session, $error) = Net::SNMP->session(
-version => 'snmpv2c',
-nonblocking=> 1,
-maxmsgsize => 6500,
-hostname => $device,
-community => $community
);
if (! defined($session)) {
printf("SNMP error session til $device med community $community\n");
exit 1;
}
$result = $session->get_bulk_request(
-callback => [\&buildTable, {}],
-maxrepetitions => 10,
-varbindlist => [$ciscoMibList]
);
snmp_dispatcher();
print "Now im there\n";
return \@miblist
}
sub buildTable {
my ($session, $table) = @_;
print "Now im here\n";
if (! defined($session->var_bind_list)) {
printf("ERROR 3: %s\n", $session->error);
exit 1;
}
my $next;
foreach my $oid (oid_lex_sort(keys(%{$session->var_bind_list}))) {
if (! oid_base_match($ciscoMibList, $oid)) {
$next = undef;
last;
}
$next = $oid;
$table->{$oid} = $session->var_bind_list->{$oid};
}
# Hvis $next er defineret er listen ikke faerdig og
# der skal hentes flere objekter.
if ( defined($next)) {
$result = $session->get_bulk_request(
-callback => [\&buildTable, $table],
-maxrepetitions => 10,
-varbindlist => [$next]
);
if(! defined($result)) {
printf("ERROR: %s\n",$session->error);
}
} else {
# Tabellen er færdig..
foreach my $oid (oid_lex_sort(keys(%{$table}))) {
$table->{$oid} =~ m/SUPPORTS\s+(\S+)/s;
push(@miblist,$1);
}
}
}
############################################
# sub: checkdepend
#
# Parser mibfil og finder dependencies som
# skal v¦re i MIBLIB f¦r compilering
############################################
sub checkdepend {
my $site = shift;
my @filelist = @{$_[0]};
my $file;
my $line;
my @depend;
my $dependcou=0;
# Check f¦rst at alle filer er der. Hvis ikke hent dem
foreach $file (@filelist) {
if ( ! -r "$MIBLIB/$file.my" ) {
$depend[$dependcou] = $file;
$dependcou++;
}
}
if (defined $depend[0]) {
getmibfiles($site, \@depend);
}
foreach $file (@filelist) {
open FILE, "$MIBLIB/$file.my" or die "Kan ikke bne filen $!\n";
while ( defined( $line = <FILE>) ) {
if ( $line =~ m/^IMPORTS/ ) {
while ( defined( $line = <FILE>) ) {
if ( $line =~ m/FROM\s+(\S+)/) {
$1 =~ /(\S+?);?$/; # Fjern evt ;
$depend[$dependcou] = $1; # fandt afh¦ngigt modul
$dependcou++;
}
}
}
}
}
if (defined $depend[0]) {
getmibfiles($site, \@depend);
}
}
############################################
# sub: getmibfiles
#
# Hent mibfiler via nettet til MIBLIB. Hvis de
# ikke eksisterer i forvejen.
# Syntax i MIBLIB:
# SITE URL PROTOCOL SNMPVERSION BIBLIOTEK [USERNAME[PASSWORD]]
############################################
sub getmibfiles {
my $site = shift; # Entry i SITECONFIG konfigurationsfil
my @filelist = @{$_[0]};# Filer som skal downloades fra site
my $url;
my $protocol; # Kun ftp er supporteret indtil videre
my $snmpver; # Version 1,2 eller 3 (Anvendes ikke endnu)
my $lib; # Bibliotek p¦ ftp-server
# Get site info
open FILE, $SITECONFIG or die "Kan ikke ¦bne filen $SITECONFIG: $!\n";
my $line;
while ( defined( $line = <FILE>) && !defined($lib)) {
eval {
if ( $line =~ m/^[^#\n].*/ ) {
$line =~ m/(\S+)\s+(\S+)\s(\S+)\s+(\S+)\s+(\S+)/;
if ( $1 eq $site ) {
$url=$2;
$protocol = $3;
$snmpver = $4;
$lib = $5;
}
}
};
if ($@) {
print "Der er sket en fejl ved parsing af ",$SITECONFIG;
die "\nRaporteret fejl: $@\n";
}
}
if ( ! defined $lib ) {
print "Kan ikke finde linie $site i $SITECONFIG\n";
}
close FILE or die "Kan ikke lukke fil: $!\n";
#
eval {
my $ftp = Net::FTP->new($url, Debug => 1, Passive => 1)
or die "Cannot connect to $site with url $url: $@";
$ftp->login("anonymous",'heth@mercantec.dk')
or die "Cannot login ", $ftp->message;
$ftp->cwd($lib)
or die "Cannot change working directory ", $ftp->message;
my @a = $ftp->dir();
foreach (@a) {
print $_,"\n";
}
#exit;
foreach (@filelist) {
if ( ! -r "$MIBLIB/$_.my" ) {
printf "Downloading [%30.30s]...........",$_;
$ftp->get("$_.my","$MIBLIB/$_.my")
or die "get failed ", $ftp->message;
#print "DONE in $ftp->transferTime() seconds\n";
}
}
$ftp->quit;
};
if ($@) {
print "Der er sket en fejl ved download fra $site url = $url";
die "\nRaporteret fejl: $@\n";
}
}