From: Michael Torrie on
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.
From: Michael Torrie on
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.
From: Steve Holden on
Booter wrote:
> 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.
>
>>> 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/

From: Frank Millman on

"Booter" <colo.avs96(a)gmail.com> wrote in message
news:ec6d247c-a6b0-4f33-a36b-1d33eace642f(a)k19g2000yqn.googlegroups.com...
> 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

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


From: Michael Torrie on
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!