http://www.perlmonks.org?node_id=552531

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

What is the best way of finding the Ethernet address? I looked at a CPAN module, but noticed it had the code:
qx(ipconfig /all)
embedded in it. Yuck!

I need a solution that works on Solaris, Windows XP, Windows 2000, Linux, and Mac OS X (I just threw the last one in, but why not? I do want a universal solution)

Is there some Perl module that does this in a fairly clean way? Can you use "getbyaddr" or some other Perl function to do this? My first preference is to use a built in Perl function, My next preference is to use a standard Perl module (the ones that come with Perl when you install it).

Replies are listed 'Best First'.
Re: Finding the Ethernet Address
by socketdave (Curate) on May 30, 2006 at 16:18 UTC
    Net::Address::Ethernet seems to use a variety of methods to grab the hardware address of your ethernet card. It works on my Windows 2k laptop and on Linux. I'm guessing that the other systems you specify will work too.

    Update: I thought on it a bit more and realized that this is probably the module that you looked at on CPAN. As other monks have pointed out, there isn't really a universal solution. This module can use ipconfig, ifconfig and arp to get the hardware address, which should cover a really large percentage of the operating systems (or rather OS installations) that run Perl.
Re: Finding the Ethernet Address
by sgifford (Prior) on May 30, 2006 at 16:17 UTC
Re: Finding the Ethernet Address
by gellyfish (Monsignor) on May 30, 2006 at 16:03 UTC

    There isn't a universal solution, this is one of those places where it really is better to call an external program.

    /J\

Re: Finding the Ethernet Address
by jdtoronto (Prior) on May 30, 2006 at 16:54 UTC
    I think that Net::Address::Ethernet is the module you are referring to. Whilst the approach of using a native config utility seems somewhat crude it does the job and requires nothing additional to the operating systems as installed. I use that module in end-user workstation type products all the time (I use the MAC address as part of the machines 'fingerprint').

    For diagnostic use where I have Perl code in a test and monitoring system for network equipment, we tend to use the Net::SNMP family. Much more capable, but at the same time, much higher overhead and you need to becertain that SNMP MIB's are present in all the machines you need to talk to, I am not sure if this is the case with all flavours of Windows.

    jdtoronto

Re: Finding the Ethernet Address
by ikegami (Patriarch) on May 30, 2006 at 16:37 UTC

    Very little is universal. Even Perl itself isn't. (Have you tried to fork on Windows? I hear you'll probably get headaches.)

    However, even if you want a less universal solution, Perl's core functions and core modules cannot do this.

Re: Finding the Ethernet Address
by qazwart (Scribe) on May 30, 2006 at 19:08 UTC
    The consensus is that there is no Perl mechanism to get the Ethernet (MAC) address, and there is no built in C library or standard mechanism to pull up this information. Net::SNMP::Interfaces would be nice, but SNMP isn't necessarily running, and I don't have priveledges to start it.

    Net::Address::Ethernet module contains OS specific external utility code which I really don't like. However, it does work for Windows, Solaris, Linux, and Mac, so this may be my only solution. There really isn't anything else.

Re: Finding the Ethernet Address
by converter (Priest) on May 31, 2006 at 11:23 UTC