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()
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
try this instead:
import subprocess
p = subprocess.Popen('ipconfig /all', shell = True, stdout =
subprocess.PIPE)
p.wait()
print p.stdout.read()
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.
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.
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/
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
Gotcha. Definitely missed that!
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
> 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?
you can try with netifaces :
http://pypi.python.org/pypi/netifaces/0.3
I use them on both Windows and Linux