Help-Site Computer Manuals
  Algorithms & Data Structures   Programming Languages   Revision Control
  Cameras   Computers   Displays   Keyboards & Mice   Motherboards   Networking   Printers & Scanners   Storage
  Windows   Linux & Unix   Mac

A convenience class around the NetSNMP perl modules.

SNMP::Class - A convenience class around the NetSNMP perl modules.


SNMP::Class - A convenience class around the NetSNMP perl modules.


This module aims to enable snmp-related tasks to be carried out with the best possible ease and expressiveness while at the same time allowing advanced features like subclassing to be used without hassle.

        use SNMP::Class;


        #create a session to a managed device -- 

        #community will default to public, version will be autoselected from 2,1

        my $s = SNMP::Class->new({DestHost => 'myhost'});    


        #modus operandi #1

        #walk the entire table

        my $ifTable = $s->walk("ifTable");

        #-more compact- 

        my $ifTable = $s->ifTable;


        #get the ifDescr.3

        my $if_descr_3 = $ifTable->object("ifDescr")->instance("3");

        #more compact

        my $if_descr_3 = $ifTable->object(ifDescr).3;

        #iterate over interface descriptions -- method senses list context and returns array

        for my $descr ($ifTable->object"ifDescr")) { 

                print $descr->get_value,"\n";



        #get the speed of the instance for which ifDescr is en0

        my $en0_speed = $ifTable->find("ifDescr","en0")->object("ifSpeed")->get_value;  


        #modus operandi #2 - list context

        while($s->ifDescr) {

                print $_->get_value;




=head1 METHODS


This method creates a new session with a managed device. At this point Version can only be 1 or 2. If Version is not present, the library will try to probe by querying sysName.0 from the device using version 2 and then version 1, whichever succeeds first. This method croaks if a session cannot be created. If the managed node cannot return the sysName.0 object, the method will also croak.


If called, this method will permanently deactivate usage of bulkwalk for the session. Mostly useful for broken agents, some buggy versions of Net-SNMP etc. =cut

sub deactivate_bulkwalks { my $self = shift(@_) or croak ``deactivate_bulkwalks called outside of an object context''; my $id = ident $self; $deactivate_bulkwalks{$id} = 1 ; return; }

sub getOid :RESTRICTED() {

        my $self = shift(@_) or croak "getvar called outside of an object context";

        my $oid = shift(@_) or croak "first arg to getvar (oid), missing";

        my $instance = shift(@_); #instance could be 0, so we do not check

        if (!defined($instance)) { confess "second arg to getvar (instance), missing" }

        my $id = ident $self;

        my $vars = new SNMP::VarList([$oid,$instance]) or confess "Internal Error: Could not create a new SNMP::VarList for $oid.$instance";

        my @a = $session{$id}->get($vars);

        ###print Dumper(@a);

        croak $session{$id}->{ErrorStr} if ($session{$id}->{ErrorNum} != 0);

        croak "Got error when tried to ask $session{$id}->{DestHost} for $oid.$instance" if ($a[0] eq "NOSUCHINSTANCE");

        return $a[0];



Returns the SNMP version of the session object.


A generalized walk method. Takes 1 argument, which is the object to walk. Depending on whether the session object is version 1 or 2, it will respectively try to use either SNMP GETNEXT's or GETBULK. On all cases, an SNMP::Class::ResultSet is returned. If something goes wrong, the method will croak.

One should probably also take a look at the SNMP::Class::ResultSet manpage pod to see what's possible.


Using a method call that coincides with an SNMP OBJECT-TYPE name is equivalent to issuing a walk with that name as argument. This is provided as a shortcut which can result to more easy to read programs. Also, if such a method is used in a list context, it won't return an SNMP::ResultSet object, but rather a list with the ResultSet's contents. This is pretty convenient for iterating through SNMP results using few lines of code.


Athanasios Douitsis, <aduitsis at>


Please report any bugs or feature requests to bug-snmp-class at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc SNMP::Class

You can also look for information at:


This module obviously needs the perl libraries from the excellent Net-SNMP package. Many thanks go to the people that make that package available.


Copyright 2007 Athanasios Douitsis, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.