Fix Python – Getting MAC Address

Question

Asked By – Mark Roddy

I need a cross platform method of determining the MAC address of a computer at run time. For windows the ‘wmi’ module can be used and the only method under Linux I could find was to run ifconfig and run a regex across its output. I don’t like using a package that only works on one OS, and parsing the output of another program doesn’t seem very elegant not to mention error prone.

Does anyone know a cross platform method (windows and linux) method to get the MAC address? If not, does anyone know any more elegant methods then those I listed above?

Now we will see solution for issue: Getting MAC Address


Answer

Python 2.5 includes an uuid implementation which (in at least one version) needs the mac address. You can import the mac finding function into your own code easily:

from uuid import getnode as get_mac
mac = get_mac()

The return value is the mac address as 48 bit integer.

This question is answered By – Armin Ronacher

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0