Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Query MAC address from 3com switch (final solution)

by jankovig (Novice)
on Dec 05, 2001 at 10:14 UTC ( #129534=sourcecode: print w/replies, xml ) Need Help??
Author/Contact Info
Description: This is the working code. Feel free to use/modify code to your liking.

#! /usr/local/bin/perl

eval '(exit $?0)' && eval 'exec /usr/local/bin/perl $0 ${1+"$@"}'
&& eval 'exec /usr/local/bin/perl $0 $argv:q'
if 0;

#    The following program automatically gets a list of MAC 
#    addresses on a 3com switch and which port each address 
#    is on using Ethernet MIB:dot1dTpFdbTable.
#    NOTE:  Portions of this code used from David M. Town <dtown@cpan.

use strict;
use Net::SNMP qw(snmp_dispatcher oid_lex_sort);

#=== Setup session to remote host ===
my ($session, $error) = Net::SNMP->session(
   -hostname  => $ARGV[0] || 'localhost',
   -community => $ARGV[1] || 'public',
   -port      => $ARGV[2] || 161

#=== Was the session created? ===
if (!defined($session)) {
   printf("ERROR: %s\n", $error);
   exit 1;

#=== OIDs queried to retrieve information ====
my $TpFdbAddress = '';
my $TpFdbPort    = '';

#=== Print the returned MAC addresses ===
printf("\n== MAC Addresses: %s ==\n\n", $TpFdbAddress);

my $result;

if (defined($result = $session->get_table(-baseoid => $TpFdbAddress)))
+ {
   foreach (oid_lex_sort(keys(%{$result}))) {
      printf("%s => %s\n", $_, $result->{$_});
   print "\n";
} else {
   printf("ERROR: %s\n\n", $session->error());

#=== Print the returned MAC ports ===
printf("\n== MAC Ports: %s ==\n\n", $TpFdbPort);

my $result;

if (defined($result = $session->get_table(-baseoid => $TpFdbPort))) {
   foreach (oid_lex_sort(keys(%{$result}))) {
      printf("%s => %s\n", $_, $result->{$_});
   print "\n";
} else {
   printf("ERROR: %s\n\n", $session->error());

#=== Close the session and exit the program ===
exit 0;
Replies are listed 'Best First'.
Re: Query MAC address from 3com switch (final solution)
by Anonymous Monk on Apr 10, 2003 at 07:26 UTC
    No need to declare 'snmp_dispatcher' because you don't use non-blocking SNMP request.
      At me switch are incorporated in a stack and on it of number of ports I happen more than 80, how to make what numbers were real?
      Hi, when iam executing the code, its working fine but there is a problem in displaying the mac address. iam getting an out put>v>E>>>Y> i had tried the code on hp switch ...... iam a student, extracting the mac address of the devices connected to Hp switch is a part of my project. i appreciate anyone to help me in this matter. thanks............

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (None)
    As of 2022-01-26 06:45 GMT
    Find Nodes?
      Voting Booth?
      In 2022, my preferred method to securely store passwords is:

      Results (69 votes). Check out past polls.