Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63839

Re: How to get Mac address of ethernet port?

Subject Re: How to get Mac address of ethernet port?
From William Ray Wing <wrw@mac.com>
Date 2014-01-13 10:42 -0500
References <6a5ceb3f-021d-4acc-b618-ce53530fa2dd@googlegroups.com> <BLU0-SMTP4386E5DFE51C2E3BD49827591B20@phx.gbl> <52D1729B.7050906@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5413.1389631361.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Jan 11, 2014, at 11:34 AM, Michael Torrie <torriem@gmail.com> wrote:

> On 01/11/2014 07:35 AM, Andriy Kornatskyy wrote:
>> Sam,
>> 
>> How about this?
>> 
>> from uuid import getnode as get_mac
>> '%012x' % get_mac()
> 
> This seems to work if you have only one ethernet adapter.  Most
> computers have two (wired and wireless) adapters.
> 
> Getting a mac address is platform-specific, and the OP has not specified
> what OS he is using.
> 
> On Windows I imagine you'd have to access the WMI subsystem in Windows.
> 
> On Linux you could access the /sys/devices/virtual/net/<interface name>
> file in the sysfs filesystem.  I'm sure there are other ways.
> 
> No idea on OS X.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

There are probably several ways in OS-X, just as there are in any other UNIX system.  The one I've used is to spawn a subprocess and run the "ifconfig" command with no arguments (which doesn't require any special privileges).  This will return a string of all the network interfaces (including the loopback and firewire interfaces in addition to Ethernet and WiFi) and their config specs.  The OP would then parse this string looking for the location of the phrase "status: active" and then back up to the mac address that precedes it.  More work than using uuid, but this guarantees a current and correct answer.

>>> import string
>>> import subprocess
>>> mac_result = subprocess.Popen(['ifconfig'], stderr = subprocess.PIPE, stdout = subprocess.PIPE).communicate()[0]
>>> mac_loc = string.find(mac_result, "status: active")

...and so on.

Bill

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to get Mac address of ethernet port? Sam <lightaiyee@gmail.com> - 2014-01-11 06:26 -0800
  Re: How to get Mac address of ethernet port? Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2014-01-11 16:35 +0200
    Re: How to get Mac address of ethernet port? "James Harris" <james.harris.1@gmail.com> - 2014-01-11 15:54 +0000
      Re: How to get Mac address of ethernet port? Roy Smith <roy@panix.com> - 2014-01-11 11:29 -0500
        Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-12 08:40 +1100
          Re: How to get Mac address of ethernet port? Roy Smith <roy@panix.com> - 2014-01-11 16:55 -0500
            Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-12 09:08 +1100
            Re: How to get Mac address of ethernet port? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-01-12 11:22 -0500
  Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-12 01:38 +1100
  Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-12 01:52 +1100
  Re: How to get Mac address of ethernet port? Skip Montanaro <skip@pobox.com> - 2014-01-11 09:03 -0600
  Re: How to get Mac address of ethernet port? Michael Torrie <torriem@gmail.com> - 2014-01-11 09:34 -0700
    Re: How to get Mac address of ethernet port? Sam <lightaiyee@gmail.com> - 2014-01-11 17:25 -0800
  Re: How to get Mac address of ethernet port? William Ray Wing <wrw@mac.com> - 2014-01-13 10:42 -0500
  Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-14 03:47 +1100

csiph-web