Category: | Network |
Author/Contact Info | nikokoja@hotmail.com |
Description: | at the bottom you'll find my few attempts in using a get_table() to retrive MAC addresses and ports. When using get_request to retrieve systemUpTime etc, everything is fine, but when I try using get_table in a same manner that's where I run into trouble. |
#! /usr/local/bin/perl
use strict;
use Net::SNMP(qw(snmp_event_loop oid_lex_sort));
my ($session, $error) = Net::SNMP->session(
-hostname => shift || 'localhost',
-community => shift || 'public',
-port => shift || 161
);
if (!defined($session)) {
printf("ERROR: %s.\n", $error);
exit 1;
}
my $snmp;
my %oid = (
TpFdbAddress => "1.3.6.1.2.1.17.4.3.1.1",
TpFdbPort => "1.3.6.1.2.1.17.4.3.1.2",
);
my $sysUpTime = '1.3.6.1.2.1.1.3.0';
my $sysDescr = '1.3.6.1.2.1.1.1.0';
my $sysObjectID = '1.3.6.1.2.1.1.2.0';
my $sysContact = '1.3.6.1.2.1.1.4.0';
my $sysName = '1.3.6.1.2.1.1.5.0';
my $sysLocation = '1.3.6.1.2.1.1.6.0';
my $sysServices = '1.3.6.1.2.1.1.7.0';
#my $macAddressOID = '1.3.6.1.2.1.17.4.3.1.1';
#my $macPortOID = '1.3.6.1.2.1.17.4.3.1.2';
print "\n\n";
## =========== Get System Uptime ===============
my $result = $session->get_request(
-varbindlist => [$sysUpTime]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("sysUpTime for host '%s' is %s\n",
$session->hostname, $result->{$sysUpTime}
);
## =========== Get System Description ===============
my $result = $session->get_request(
-varbindlist => [$sysDescr]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("sysDescr for host '%s' is %s\n",
$session->hostname, $result->{$sysDescr}
);
## =========== Get System ObjectID ===============
my $result = $session->get_request(
-varbindlist => [$sysObjectID]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("sysObjectID for host '%s' is %s\n",
$session->hostname, $result->{$sysObjectID}
);
## =========== Get System Contact ===============
my $result = $session->get_request(
-varbindlist => [$sysContact]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("sysContact for host '%s' is %s\n",
$session->hostname, $result->{$sysContact}
);
## =========== Get System Name ===============
my $result = $session->get_request(
-varbindlist => [$sysName]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("sysName for host '%s' is %s\n",
$session->hostname, $result->{$sysName}
);
## =========== Get System Location ===============
my $result = $session->get_request(
-varbindlist => [$sysLocation]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("sysLocation for host '%s' is %s\n",
$session->hostname, $result->{$sysLocation}
);
## =========== Get System Services ===============
my $result = $session->get_request(
-varbindlist => [$sysServices]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("sysServices for host '%s' is %s\n",
$session->hostname, $result->{$sysServices}
);
## =========== Get MAC Addresses ===============
#my $result = $session->get_request(
# -varbindlist => [$macAddressOID]
#);
#
#if (!defined($result)) {
# printf("ERROR: %s.\n", $session->error);
# $session->close;
# exit 1;
#}
#printf("sysServices for host '%s' is %s\n",
# $session->hostname, $result->{$macAddressOID}
#);
## =========== Get MAC Ports ===============
#my $result = $session->get_request(
# -varbindlist => [$macPortOID]
#);
#if (!defined($result)) {
# printf("ERROR: %s.\n", $session->error);
# $session->close;
# exit 1;
#}
#printf("sysServices for host '%s' is %s\n",
# $session->hostname, $result->{$macPortOID}
#);
## ============= Added for loop to try and handle get_table() ==
for (keys %oid) {
if (defined($snmp{response} = $snmp{session} -> get_table($oid{$
+_})))
{
for (oid_lex_sort(keys(%{$snmp{response}})))
{
print($snmp{response} -> {$_}, ",\n");
}
} else
{
print($snmp{session} -> error(), ",\n");
}
}
}
print "\n\n";
$session->close;
exit 0;
|
Back to
Code Catacombs