1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#! /bin/env perl
# ============================================================================
# $Id: table.pl,v 6.0 2009/09/09 15:05:33 dtown Rel $
# Copyright (c) 2000-2009 David M. Town <dtown@cpan.org>
# All rights reserved.
# This program is free software; you may redistribute it and/or modify it
# under the same terms as the Perl 5 programming language system itself.
# ============================================================================
use strict;
use warnings;
use Net::SNMP qw( snmp_dispatcher SNMP_PORT );
# Create the SNMP session
my ($session, $error) = Net::SNMP->session(
-hostname => $ARGV[0] || 'localhost',
-community => $ARGV[1] || 'public',
-port => $ARGV[2] || SNMP_PORT,
-version => 'snmpv2c',
);
# Was the session created?
if (!defined $session) {
printf "ERROR: %s.\n", $error;
exit 1;
}
# iso.org.dod.internet.mgmt.interfaces.ifTable
my $OID_ifTable = '1.3.6.1.2.1.2.2';
printf "\n== SNMPv2c blocking get_table(): %s ==\n\n", $OID_ifTable;
my $result;
if (defined ($result = $session->get_table(-baseoid => $OID_ifTable))) {
for ($session->var_bind_names()) {
my $oid = $_;
$oid =~ s/ +//;
printf "%s => %s\n", $oid, $result->{$_};
}
print "\n";
} else {
printf "ERROR: %s.\n\n", $session->error();
}
$session->close();
###
## Now a non-blocking example
###
printf "\n== SNMPv2c non-blocking get_table(): %s ==\n\n", $OID_ifTable;
# Blocking and non-blocking objects cannot exist at the
# same time. We must clear the reference to the blocking
# object or the creation of the non-blocking object will
# fail.
$session = undef;
# Create the non-blocking SNMP session
($session, $error) = Net::SNMP->session(
-hostname => $ARGV[0] || 'localhost',
-community => $ARGV[1] || 'public',
-port => $ARGV[2] || SNMP_PORT,
-nonblocking => 1,
-version => 'snmpv2c',
);
# Was the session created?
if (!defined $session) {
printf "ERROR: %s.\n", $error;
exit 1;
}
if (!defined $session->get_table(-baseoid => $OID_ifTable,
-callback => \&print_results_cb))
{
printf "ERROR: %s.\n", $session->error();
}
# Start the event loop
snmp_dispatcher();
print "\n";
exit 0;
sub print_results_cb
{
my ($session) = @_;
if (!defined $session->var_bind_list()) {
printf "ERROR: %s.\n", $session->error();
} else {
for ($session->var_bind_names()) {
my $oid = $_;
$oid =~ s/ +//;
printf "%s => %s\n", $oid, $session->var_bind_list()->{$_};
}
}
return;
}
# ============================================================================
|