Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Accessing NIC/mac address ?

by PrimeLord (Pilgrim)
on Feb 14, 2002 at 17:12 UTC ( #145502=note: print w/replies, xml ) Need Help??


in reply to Accessing NIC/mac address ?

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.

Replies are listed 'Best First'.
Re: Re: Accessing NIC/mac address ?
by earthboundmisfit (Chaplain) on Feb 14, 2002 at 17:26 UTC
    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!
        I was not aware of the /all switch. Thanks! That's very useful.

        Just to round out the thread:

        #! Perl -w use strict; open (READ, "ipconfig /all|") or die "$!"; while (<READ>) { chomp; if (/Physical Address/) { my @pieces = split(/:/); print $pieces[1]; } } close READ;
Re: Re: Accessing NIC/mac address ?
by rob_au (Abbot) on Feb 15, 2002 at 03:50 UTC
    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&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-09-25 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?