Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

obtaining MAC address(es)

by dshahin (Pilgrim)
on Jan 26, 2001 at 01:10 UTC ( [id://54362]=perlquestion: print w/replies, xml ) Need Help??

dshahin has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for the most cross-platform way to determine the MAC address, or addresses via a perl-script, without resorting to system() calls.

Is there a reasonable way to get at such information using just Perl?
If not, can anyone suggest the most portable way of obtaining this piece of info?

Replies are listed 'Best First'.
Re: obtaining MAC address(es)
by saucepan (Scribe) on Jan 26, 2001 at 02:28 UTC
    Tricky one! I tried having a peek at my (FreeBSD) ifconfig sources to see what it might be up to, but found it impenetrable to casual inspection by someone with my modest C skills. :(

    The most portable method I could come up with is, unfortunately:

    sub unique { keys %{{ map { $_, 1 } @_ }} } my $ifconfig; -x and $ifconfig = $_, last for qw(/sbin/ifconfig /bin/ifconfig); die "Can't find ifconfig\n" unless $ifconfig; my @hwaddrs = `$ifconfig` =~ /\b((?:[a-f\d]{2}:){5}[a-f\d]{2})\b/gi; { local ($\, $,) = ("\n", "\n") and print unique @hwaddrs }
    ..and this will only work on a Unix system that uses ifconfig (and possibly only on Linux and FreeBSD, on which I was able to test).

    Hopefully this could serve as a start if you decide to do as Fastolfe suggested and write a module that provides this service portably. :)

    If what you are actually looking for is a host-unique ID of some kind, you might be able to get away with using the host's default IP address as the ID, depending on the extent of your persistence (DHCP), global uniqueness (RFC 1918) and security requirements.

    update: minor cleanup of ugly regex
    update: my a-f got changed to a-z somewhere along the line (fixed)

Re: obtaining MAC address(es)
by lhoward (Vicar) on Jan 26, 2001 at 01:38 UTC
    What are you looking to find the MAC address of?

    If you're looking to find the MAC address of all interfaces the box you're running the script on what I'd consider the most cross-platform solution is to use one of perl's SNMP modules to query the ifTable ifPhysAddress entries for all the interfaces on the box. Of course, this assumes that all boxes you're gonnna run this script on have an SNMP daemon/service running on them and that you know the community string.

      It really depends on the environment youre trying to obtain an address on. As I understand it, the IPv6 protocol includes MAC Address information (very cool, but spoofable). This may be of limited use to you depending on your application. I am moving to IPv6 on my internal network here to see how it behaves. You may be able to do something similar, in which case you could probably just take a look at the packets coming back from the machine in question.

      dep

      --
      i am not cool enough to have a signature.

Re: obtaining MAC address(es)
by Fastolfe (Vicar) on Jan 26, 2001 at 01:59 UTC
    Unfortunately to obtain details about a system's configuration, even if multiple operating systems share common pieces of hardware, you're going to have to resort to something pretty OS-specific. At this point you're not so much doing "application things" but querying the system itself, so the mechanism is going to have to be OS-specific. If you can't find a common method for retrieving this information (SNMP was mentioned), you're going to have to find or write a module to do this in a portable way, using different methods depending upon what's available.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54362]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-19 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found