Net::SNMP – Non-blocking SNMPv2c get-bulk-request for ifTable

Net::SNMP –

3. Non-blocking SNMPv2c get-bulk-request for ifTable

This example gets the contents of the ifTable by sending get-bulk-requests until the responses are no longer part of the ifTable. The ifTable can also be retrieved using the get_table() method. The ifPhysAddress object in the table has a syntax of an OCTET STRING. By default, translation is enabled and non-printable OCTET STRINGs are translated into a hexadecimal format. Sometimes the OCTET STRING contains all printable characters and this produces unexpected output when it is not translated. The example turns off translation for OCTET STRINGs and specifically formats the output for the ifPhysAddress objects.

   #! /usr/local/bin/perl

   use strict;
   use warnings;

   use Net::SNMP qw(:snmp);

   my $OID_ifTable = '';
   my $OID_ifPhysAddress = '';

   my ($session, $error) = Net::SNMP->session(
      -hostname    => shift || 'localhost',
      -community   => shift || 'public',
      -nonblocking => 1,
      -translate   => [-octetstring => 0],
      -version     => 'snmpv2c',

   if (!defined $session) {
      printf "ERROR: %s.\n", $error;
      exit 1;

   my %table; # Hash to store the results

   my $result = $session->get_bulk_request(
      -varbindlist    => [ $OID_ifTable ],
      -callback       => [ \&table_callback, \%table ],
      -maxrepetitions => 10,

   if (!defined $result) {
      printf "ERROR: %s\n", $session->error();
      exit 1;

   # Now initiate the SNMP message exchange.



   # Print the results, specifically formatting ifPhysAddress.

   for my $oid (oid_lex_sort(keys %table)) {
      if (!oid_base_match($OID_ifPhysAddress, $oid)) {
         printf "%s = %s\n", $oid, $table{$oid};
      } else {
         printf "%s = %s\n", $oid, unpack 'H*', $table{$oid};

   exit 0;

   sub table_callback
      my ($session, $table) = @_;

      my $list = $session->var_bind_list();

      if (!defined $list) {
         printf "ERROR: %s\n", $session->error();

      # Loop through each of the OIDs in the response and assign
      # the key/value pairs to the reference that was passed with
      # the callback. Make sure that we are still in the table
      # before assigning the key/values.

      my @names = $session->var_bind_names();
      my $next  = undef;

      while (@names) {
         $next = shift @names;
         if (!oid_base_match($OID_ifTable, $next)) {
            return; # Table is done.
         $table->{$next} = $list->{$next};

      # Table is not done, send another request, starting at the last
      # OBJECT IDENTIFIER in the response. No need to include the
      # calback argument, the same callback that was specified for the
      # original request will be used.

      my $result = $session->get_bulk_request(
         -varbindlist    => [ $next ],
         -maxrepetitions => 10,

      if (!defined $result) {
         printf "ERROR: %s.\n", $session->error();

This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Net::SNMP – Non-blocking SNMPv2c get-bulk-request for ifTable

  1. Pingback: Translate octetstring | Babystepsandbe

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s