Getting Local MAC Address

B

Booter

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
 
D

danmcleran

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

for windows parse p.stdout.read():

import subprocess

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

p.wait()

print p.stdout.read()
 
D

danmcleran

for windows parse p.stdout.read():

import subprocess

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

p.wait()

print p.stdout.read()

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

danmcleran

for windows parse p.stdout.read():

import subprocess

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

p.wait()

print p.stdout.read()

try this instead:

import subprocess

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

p.wait()

print p.stdout.read()
 
I

Irmen de Jong

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

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

-irmen
 
M

Michael Torrie

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.
 
M

Michael Torrie

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.
 
S

Steve Holden

Booter said:
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.

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
 
F

Frank Millman

Booter said:
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
 
B

Booter

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
 
L

Lawrence D'Oliveiro

In message
Booter said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top