Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Query a MAC address

by jankovig (Novice)
on Dec 02, 2001 at 04:58 UTC ( #128934=sourcecode: print w/ replies, xml ) Need Help??

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;

Comment on Query a MAC address
Download Code
Re: Query a MAC address
by ehdonhon (Curate) on Dec 02, 2001 at 05:05 UTC

    Suggestion:

    Unless you're trying to win an award for obfu or something, please enclose your code with the <code> tags. That will cause it to be formatted correctly in html. For more information, please look here.

Re: Query a MAC address
by Fastolfe (Vicar) on Dec 02, 2001 at 05:22 UTC

    If you have a question, please post it to Seekers of Perl Wisdom along with a better description of the problem.

    I notice your code has a lot of repetition. Generally, whenever I see repetition in code, that tells me I can probably reduce some of it to a function or iteration:

    my %check_oids = ( sysUpTime => '1.3.6.1.2.1.1.3.0', sysDescr => '1.3.6.1.2.1.1.1.0', sysObjectID => '1.3.6.1.2.1.1.2.0', sysContact => '1.3.6.1.2.1.1.4.0', sysName => '1.3.6.1.2.1.1.5.0', sysLocation => '1.3.6.1.2.1.1.6.0', sysServices => '1.3.6.1.2.1.1.7.0', ); foreach (keys %check_oids) { my $result = $session->get_request( -varbindlist => [$check_oids{$_}] ); if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } printf("%s for host '%s' is %s\n", $_, $session->hostname, $result->{$check_oids{$_}} ); }

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://128934]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-08-30 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls