Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (5)
    As of 2020-11-28 23:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?