PyCharm is a great editor for developing Python code in Linux, and there are two versions available. The community version is for the casual developer, whereas the professional environment provides all the tools a developer could need for creating professional software.
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?
Greg Mattes13 Answers
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:
The return value is the mac address as 48 bit integer.
Armin RonacherArmin RonacherThe pure python solution for this problem under Linux to get the MAC for a specific local interface, originally posted as a comment by vishnubob and improved by on Ben Mackey in this activestate recipe
This is the Python 3 compatible code:
synthesizerpatelsynthesizerpatelnetifaces is a good module to use for getting the mac address (and other addresses). It's crossplatform and makes a bit more sense than using socket or uuid.
Peter TurnerOne other thing that you should note is that uuid.getnode()
can fake the MAC addr by returning a random 48-bit number which may not be what you are expecting. Also, there's no explicit indication that the MAC address has been faked, but you could detect it by calling getnode()
twice and seeing if the result varies. If the same value is returned by both calls, you have the MAC address, otherwise you are getting a faked address.
Sometimes we have more than one net interface.
A simple method to find out the mac address of a specific interface, is:
to call the method is simple
Julio SchurtJulio SchurtUsing my answer from here: https://stackoverflow.com/a/18031868/2362361
It would be important to know to which iface you want the MAC for since many can exist (bluetooth, several nics, etc.).
This does the job when you know the IP of the iface you need the MAC for, using netifaces
(available in PyPI):
Testing:
You can do this with psutil which is cross-platform:
evandrixNote that you can build your own cross-platform library in python using conditional imports. e.g.
This will allow you to use os.system calls or platform-specific libraries.
John FouhyJohn FouhyThe cross-platform getmac package will work for this, if you don't mind taking on a dependency. It works with Python 2.7+ and 3.4+. It will try many different methods until either getting a address or returning None.
Disclaimer: I am the author of the package.
Update (Jan 14 2019): the package now only supports Python 2.7+ and 3.4+. You can still use an older version of the package if you need to work with an older Python (2.5, 2.6, 3.2, 3.3).
I dont know of a unified way, but heres something that you might find useful:
What I would do in this case would be to wrap these up into a function, and based on the OS it would run the proper command, parse as required and return only the MAC address formatted as you want. Its ofcourse all the same, except that you only have to do it once, and it looks cleaner from the main code.
For Linux let me introduce a shell script that will show the mac address and allows to change it (MAC sniffing).
Cut arguements may dffer (I am not an expert) try:
To change MAC we may do:
will change mac address to 00:80:48:BA:d1:30 (temporarily, will restore to actual one upon reboot).
Bill the LizardPython Editor Notebook For Machine Learning Ipython
For Linux you can retrieve the MAC address using a SIOCGIFHWADDR ioctl.
You've tagged the question 'python'. I don't know of an existing Python module to get this information. You could use ctypes to call the ioctl directly.
DGentryDGentryBest Editor For Mac
protected by hjpotter92Jan 24 '14 at 10:36
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?