Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: MAC Address of An Interface

by Rhys (Pilgrim)
on Sep 08, 2004 at 12:00 UTC ( #389317=note: print w/ replies, xml ) Need Help??


in reply to MAC Address of An Interface

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. ;-)


Comment on Re: MAC Address of An Interface
Select or Download Code
Re^2: MAC Address of An Interface
by sintadil (Pilgrim) on Sep 09, 2004 at 01:31 UTC

    ++ for platform-independent code. I actually took a few moments to try to come up with a shell-out solution, but wouldn't you know it, the command-line tools all work differently on different platforms. I'd ++ you a dozen times more, if I could.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-09-01 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (299 votes), past polls