Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Accessing NIC/mac address ?

by peterg22 (Novice)
on Feb 14, 2002 at 16:55 UTC ( [id://145496]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
I am looking for a way to find out the mac address of the network card in my PC using Perl. I am interested in encrypting data prior to storing it in a database, and was considering using host machine's mac address as part of the pass phrase. I believe that it would make for an added level of security during decryption as well. I have had a good look around CPAN, but I'm not aware of anything that talks directly to hardware. Has anyone else managed to do this ?


Mildew Hall.. Home of PurePostPro and other Perl goodies!
Not only oysters create Pe[a]rls

Replies are listed 'Best First'.
Re: Accessing NIC/mac address ?
by PrimeLord (Pilgrim) on Feb 14, 2002 at 17:12 UTC
    Well depending on your OS there are a couple of ways you can do this. If it is a *nix machine you can do smoething like.
    open (READ, "ifconfig fxp0 |") or die "$!"; while (<READ>) { chomp; if (/ether/) { s/^\s+//; my $mac = (split)[1]; } } close READ;
    I haven't tested this code, but I am pretty sure that would do it. And if it is an NT machine you would just do something similar with ipconfig instead. Note you should replace fxp0 with the name of your network device. Update: I just tested the code and it worked.
      update: arp gets the MAC address of the gateway or router of your network. I should just shut up.

      Same basic idea for Win NT/2000 except you would use 'arp -a' what Rex(Wrecks) says in place of the ifconfig read

        arp -a is not that good as you get the entire arp cache. ipconfig /all is much better as it allows you to parse data per interface.

        I guess this is TIMTOWTDI, but using ipconfig /all allows you to write a reusable sub that returns all of the parsed data for you, and allows you to pick and choose which of that data you want to use. I guess I'm lazy and just do all the work up front and never have to deal with it again :)

        "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
      A better solution might be to look at the IO::Interface module as I have suggested here - The reason for this is the dependency on the external binary which your code introduces. This dependency could easily render your code unworkable if the output structure from ifconfig is modified significantly - Also too, as you have not specified a full path to the binary, it would be quite easy for someone with malicious intent to change the path environment and introduce their own ifconfig which could generate some very unexpected and most unwanted results within your code.

      I have rambled on this topic previously here.


      perl -e 's&&[@.]/&&s&.com.&_&&&print'

Re: Accessing NIC/mac address ?
by rob_au (Abbot) on Feb 15, 2002 at 03:43 UTC
    If you are running this on a *NIX based platform, you should look into the IO::Interface module - This module gives you access to a wealth of information about network interface configurations. For example, from the documentation ...

    use IO::Socket; use IO::Interface qw(:flags); my $s = IO::Socket::INET->new(Proto => 'udp'); my @interfaces = $s->if_list; for my $f (@interfaces) { print "interface = $if\n"; my $flags = $s->if_flags($if); print "addr = ",$s->if_addr($if),"\n", "broadcast = ",$s->if_broadcast($if),"\n", "netmask = ",$s->if_netmask($if),"\n", "dstaddr = ",$s->if_dstaddr($if),"\n", "hwaddr = ",$s->if_hwaddr($if),"\n"; print "is running\n" if $flags & IFF_RUNNING; print "is broadcast\n" if $flags & IFF_BROADCAST; print "is p-to-p\n" if $flags & IFF_POINTOPOINT; print "is loopback\n" if $flags & IFF_LOOPBACK; print "is promiscuous\n" if $flags & IFF_PROMISC; print "is multicast\n" if $flags & IFF_MULTICAST; print "is notrailers\n" if $flags & IFF_NOTRAILERS; print "is noarp\n" if $flags & IFF_NOARP; }

    If all that you are looking for is the MAC address of the interface card, then the $obj->if_hwaddr method is all you will need to worry about.


    perl -e 's&&[@.]/&&s&.com.&_&&&print'

      Yep, this works really well, so I will be able to start coding. Thanks to everyone that offered suggestions. I didn't want to use an external program unless there was no other way. I'll post a snippet when I have something finished.
      There is a small typo though -

      for my $f (@interfaces) {
      should be:
      for my $if (@interfaces) {
      otherwise nothing prints out..

      Mildew Hall.. Home of PurePostPro and other Perl goodies!
      Not only oysters create Pe[a]rls

Re: Accessing NIC/mac address ?
by belg4mit (Prior) on Feb 15, 2002 at 04:00 UTC
    A simple search for MAC address would have proved quite enlightening. However there does not yet appear to be a node pointing to my favorite (read the one I've used) method. Net::Interface|Net::Interface. *However*, it is my experience that the MAC address code does not work in the official version, and last I checked (late 2000) the maintainer seemed to have fallen off the Earth. So you'd have to use my patched version. (Thanks tye, IIRC for ->ppport.h).

    perl -pe "s/\b;([st])/'\1/mg"

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-22 03:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found