Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Did you say SNMP?

Or, if you want to get the MAC addresses of a whole bunch of devices at once (including any IP aliases that might be set up), you can do:

# DO NOT USE THIS WITHOUT YOUR NET ADMINS' PERMISSION! # YOU PROBABLY WON'T HURT ANYTHING, BUT YOU'LL UPSET THEM # IF YOU DON'T ASK FIRST. use SNMP; # Requires Net-SNMP package from SourceForge. use strict; my $community; my $defgate; my $mib; my $sess; my $var; my $vb; my %ARP; my %sessparms; # Initialize SNMP. $SNMP::use_sprint_value = 1; # The MIB objects we need are pre-loaded in Net-SNMP. &SNMP::initMib(); $community = 'public'; # Use your read-only community here. $defgate = '127.0.0.1'; # Use your default gateway here. $sessparms{'DestHost'} = $defgate; $sessparms{'Community'} = $community; $sessparms{'Version'} = '2'; # Recommended. $sess = new SNMP::Session(%sessparms); if ( not defined $sess ) { die "Couldn't open SNMP session to $defgate.\n"; } $mib = 'ipNetToMediaPhysAddress'; # ARP table. $vb = new SNMP::Varbind([$mib]); GETARP: while ( $vb->tag eq $mib ) { $var = $sess->getnext($vb); if ( $sess->{ErrorNum} ) { print "Got $sess->{ErrorStr} querying ARP table on $defgate.\n"; last GETARP; } if ( $vb->iid =~ /(\d+\.\d+\.\d+\.\d+)$/ ) { $ARP{$1} = $var; } } # Not the best sort for this, but hey... foreach ( sort keys %ARP ) { print sprintf "MAC for %-15s is %17s.\n", $_, $ARP{$_}; }

NOTES:

  1. As it says, this is unwise to do if you don't have the permission of whoever is running the router. You'll set off all sorts of security alarms, the DoHS will drag you away and you'll never be heard from again. {:-O
  2. If you have a bunch of machines running this script against the same router at the same time, you can bog down its CPU (although I've never seen a single querier create such a problem, and most routers set management traffic to a low priority anyway).
  3. The format of the instance numbers ($vb->iid) changes from router to router, so you may want to add a print "IID is ", $vb->iid, "\n"; in there to see for yourself if things aren't working.

Enjoy!

--J

Update: Gave a shorter while loop with a better test. Fixed arg to SNMP::Session() (a hash, not a hashref). Added test for successful session creation.

Update: Added proper 'header' line to fit the rest of the thread. ;-)


In reply to Re: MAC Address of An Interface by Rhys
in thread MAC Address of An Interface by amt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2021-09-27 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?