Net: : SNMP example 1

From Teknologisk videncenter
Jump to: navigation, search
#!/usr/bin/perl
######################################
# Author: Henrik Thomsen / EUC MIDT  #
# Email.: heth@eucmidt.dk            #
######################################
#
### Pragma's
use strict;
use warnings;
use locale;
#use diagnostics; # Only for debugging purposes.
### Module's
use Net::SNMP qw(:snmp);

my @devices = ('192.168.22.201','192.168.22.202');

my $ifTable = '1.3.6.1.2.1.2.2.1.2';

foreach my $device (@devices) {
my ($session, $error) = Net::SNMP->session(
			 -hostname => $device,
             -version  => '2c', );

  if (!defined($session)) {
    printf("Kan ikke åbne session til $device fejl: $error\n");
    next;
  }
  print "Henter interfaces fra $device\n";
  print "=============================\n";

  my $result = $session->get_next_request(
  		-varbindlist => [$ifTable] );
  my ($key,$value);
  my $lastkey;

  while (defined($result) ) {
    while ( ($key,$value) = each %{$result} ) {
      if ( !oid_base_match($ifTable, $key)) {
        $session->close;
        undef $result;
        last;
      }
     print "  OID: $key = $value\n";
     $lastkey=$key;
    }
    $result = $session->get_next_request( -varbindlist => [$lastkey] );
    print "-->";
  }
}