3 # $Id: bulkwalk.pl,v 5.0 2002/04/20 07:30:18 hardaker Exp $
8 # Hard-coded hostname and community. This is icky, but I didn't want to
9 # muddle the example with parsing command line arguments. Deal with it. -r
11 my $hostname='localhost';
13 my $community='public';
16 $SNMP::dump_packet = 0;
18 $sess = new SNMP::Session( 'DestHost' => $hostname,
19 'Community' => $community,
20 'RemotePort' => $port,
24 'UseLongNames' => 1, # Return full OID tags
25 'UseNumeric' => 1, # Return dotted decimal OID
26 'UseEnums' => 0, # Don't use enumerated vals
27 'UseSprintValue' => 0); # Don't pretty-print values
29 die "Cannot create session: ${SNMP::ErrorStr}\n" unless defined $sess;
31 # Set up a list of two non-repeaters and some repeated variables.
35 # The 'get' performed for non-repeaters is a "GETNEXT" (the non-repeater
36 # requests are not fulfilled with SNMP GET's). This means that you must
37 # ask for the lexicographically preceeding variable for non-repeaters.
39 # For most branches (i.e. 'sysUpTime'), this "just works" -- be sure you
40 # don't ask for an instance, and the response will be as expected. However,
41 # if you want a specific variable instance (i.e. 'ifSpeed.5'), you must
42 # ask for the _preceeding_ variable ('ifSpeed.4' in this example).
44 # See section 4.2.3 of RFC 1905 for more details on GETBULK PDU handling.
47 my $vars = new SNMP::VarList( ['sysUpTime'], # Nonrepeater variable
48 ['ifNumber'], # Nonrepeater variable
49 ['ifSpeed'], # Repeated variable
50 ['ifDescr'] ); # Repeated variable.
52 # Do the bulkwalk of the two non-repeaters, and the repeaters. Ask for no
53 # more than 8 values per response packet. If the caller already knows how
54 # many instances will be returned for the repeaters, it can ask only for
55 # that many repeaters.
57 @resp = $sess->bulkwalk(2, 8, $vars);
58 die "Cannot do bulkwalk: $sess->{ErrorStr} ($sess->{ErrorNum})\n"
61 # Print out the returned response for each variable.
62 for $vbarr ( @resp ) {
63 # Determine which OID this request queried. This is kept in the VarList
64 # reference passed to bulkwalk().
65 $oid = $$vars[$i++]->tag();
67 # Count the number of responses to this query. The count will be 1 for
68 # non-repeaters, 1 or more for repeaters.
69 $num = scalar @$vbarr;
70 print "$num responses for oid $oid: \n";
72 # Display the returned list of varbinds using the SNMP::Varbind methods.
74 printf("\t%s = %s (%s)\n", $v->name, $v->val, $v->type);
80 # Now do the same bulkwalk again, but in asynchronous mode. Set up a Perl
81 # callback to receive the reference to the array of arrays of Varbind's for
82 # the return value, and pass along the $vars VarList to it. This allows us
83 # to print the oid tags (the callback code is almost the same as above).
85 # First, define the Perl callback to be called when the bulkwalk completes.
86 # The call to SNMP::finish() will cause the SNMP::MainLoop() to return once
87 # the callback has completed, so that processing can continue.
90 my ($vars, $values) = @_;
92 for $vbarr ( @$values ) {
93 # Determine which OID this request queried. This is kept in the
94 # '$vars' VarList reference passed to the Perl callback by the
95 # asynchronous callback.
96 $oid = (shift @$vars)->tag();
98 # Count the number of responses to this query. The count will be 1 for
99 # non-repeaters, 1 or more for repeaters.
100 $num = scalar @$vbarr;
101 print "$num responses for oid $oid: \n";
103 # Display the returned list of varbinds using the SNMP::Varbind methods.
105 printf("\t%s = %s (%s)\n", $v->name, $v->val, $v->type);
113 # The actual bulkwalk request is done here. Note that the $vars VarList
114 # reference will be passed to the Perl callback when the bulkwalk completes.
116 my $reqid = $sess->bulkwalk(2, 8, $vars, [ \&callback, $vars ]);
117 die "Cannot do async bulkwalk: $sess->{ErrorStr} ($sess->{ErrorNum})\n"
118 if $sess->{ErrorNum};
120 # Now drop into the SNMP event loop and await completion of the bulkwalk.
121 # The call to SNMP::finish() in &callback will make the SNMP::MainLoop()
122 # return to the caller.