Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting Local MAC Address

431 views
Skip to first unread message

Booter

unread,
Apr 2, 2010, 4:14:32 PM4/2/10
to
Hello all,

I am new to python ans was wondering if there was a way to get the mac
address from the local NIC?

Thanks for your help.

Gerad

danmc...@yahoo.com

unread,
Apr 2, 2010, 4:52:42 PM4/2/10
to

for windows parse p.stdout.read():

import subprocess

p = subprocess.Popen('ipconfig', shell = True, stdout =
subprocess.PIPE)

p.wait()

print p.stdout.read()

danmc...@yahoo.com

unread,
Apr 2, 2010, 4:55:54 PM4/2/10
to
On Apr 2, 2:52 pm, "danmcle...@yahoo.com" <danmcle...@yahoo.com>
wrote:

sorry, posted too soon. looks like this is for ip address only.

Irmen de Jong

unread,
Apr 2, 2010, 4:59:41 PM4/2/10
to

Actually you can get more info including the MAC address when you pass
the /all switch.

-irmen

danmc...@yahoo.com

unread,
Apr 2, 2010, 4:58:35 PM4/2/10
to
On Apr 2, 2:52 pm, "danmcle...@yahoo.com" <danmcle...@yahoo.com>
wrote:

try this instead:

import subprocess

p = subprocess.Popen('ipconfig /all', shell = True, stdout =
subprocess.PIPE)

p.wait()

print p.stdout.read()

Michael Torrie

unread,
Apr 2, 2010, 5:48:31 PM4/2/10
to pytho...@python.org, danmc...@yahoo.com
On 04/02/2010 03:30 PM, Dan McLeran wrote:
> i'm running python 2.6 on win xp sp3 and i get:

Your code isn't portable to non-Windows OS's. On my Mac and on my Linux
workstations it simply doesn't work. Using '/usr/sbin/ifconfig' as the
executable name in Popen does work, however.

The OP didn't state his platform, so we shouldn't assume that a
windows-only solution will work for him may.

Since this list covers the use of many kinds of operating systems, it is
foolish to make assumptions. This was my point.

Michael Torrie

unread,
Apr 2, 2010, 5:51:26 PM4/2/10
to Booter, pytho...@python.org
On 04/02/2010 02:14 PM, Booter wrote:
> I am new to python ans was wondering if there was a way to get the mac
> address from the local NIC?

As Dan has indicated, you have to Popen an external command to get this
information. Every OS has different commands and syntaxes for this.
You'll have to have a different Popen for each operating system. Also
you must take into account that most computers have more than one
ethernet interface these days (real and virtual). So you'll likely end
up with between 2 and 5 different MAC addresses. And some of those are
fake as well, like the MAC addresses used by VMware's virtual networking
interfaces.

What operating system are you targeting? Windows? Linux? Mac? To
really answer your question you must supply more information.

Steve Holden

unread,
Apr 2, 2010, 6:18:36 PM4/2/10
to pytho...@python.org
>>> import uuid
>>> uuid.getnode()
246090452741227L
>>>

This is supposed to return the MAC address, but I am not sure it does.
The documentation says:

"""
getnode( )

Get the hardware address as a 48-bit positive integer. The first time
this runs, it may launch a separate program, which could be quite slow.
If all attempts to obtain the hardware address fail, we choose a random
48-bit number with its eighth bit set to 1 as recommended in RFC 4122.
"Hardware address" means the MAC address of a network interface, and on
a machine with multiple network interfaces the MAC address of any one of
them may be returned.
"""

So the return value isn't *guaranteed* to be an ethernet address, and
I'm not sure whether that code gets any regular testing.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

Frank Millman

unread,
Apr 3, 2010, 2:25:26 AM4/3/10
to pytho...@python.org

"Booter" <colo....@gmail.com> wrote in message
news:ec6d247c-a6b0-4f33...@k19g2000yqn.googlegroups.com...

This is what I use -

------------------------
def get_mac_address():
if sys.platform == 'win32':
for line in os.popen("ipconfig /all"):
if line.lstrip().startswith('Physical Address'):
mac = line.split(':')[1].strip().replace('-',':')
break
else:
# mac = os.popen("/sbin/ifconfig|grep Ether|awk {'print
$5'}").read()[:-1]
for line in os.popen("/sbin/ifconfig"):
if 'Ether' in line:
mac = line.split()[4]
break
return mac

------------------------

I only target windows and linux. I don't know if it works for all platforms.

I wrote this a long time ago. I think it would now be preferable to use
subprocess() instead of os.popen().

Note the commented-out line in the linux block. This is an alternative
method I cribbed from somewhere. Not as readable, but probably faster.

HTH

Frank Millman


Michael Torrie

unread,
Apr 2, 2010, 6:56:26 PM4/2/10
to pytho...@python.org
On 04/02/2010 04:01 PM, Dan McLeran wrote:
> which is why my OP stated the solution was for windows:
>
> "for windows parse
> p.stdout.read():"

Gotcha. Definitely missed that!

Booter

unread,
Apr 5, 2010, 12:07:48 PM4/5/10
to
All,

Thanks for all of the great solutions! Sorry I wasn't more specific
in my post and will keep that in mind for future posts. Just FYI I
was using a Windows machine and running Python 2.6.

Once again thanks for all of your help!

Gerad

Lawrence D'Oliveiro

unread,
Apr 6, 2010, 3:34:47 AM4/6/10
to
In message
<ec6d247c-a6b0-4f33...@k19g2000yqn.googlegroups.com>, Booter
wrote:

> I am new to python ans was wondering if there was a way to get the mac
> address from the local NIC?

What if you have more than one?

Rebelo

unread,
Apr 7, 2010, 3:32:01 AM4/7/10
to


you can try with netifaces :
http://pypi.python.org/pypi/netifaces/0.3
I use them on both Windows and Linux

0 new messages