Python Editor Notebook For Mac

Notebook

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.

Activetoday

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 Mattes
21.2k13 gold badges60 silver badges101 bronze badges
Mark RoddyMark Roddy
16.2k14 gold badges58 silver badges67 bronze badges

13 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 Ronacher
27.1k10 gold badges60 silver badges66 bronze badges

The 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:

synthesizerpatelsynthesizerpatel
20.9k4 gold badges61 silver badges79 bronze badges

netifaces 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 Turner
5,1778 gold badges57 silver badges104 bronze badges
camflancamflan
12.2k3 gold badges18 silver badges17 bronze badges

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

mhawkemhawke
62k8 gold badges69 silver badges94 bronze badges

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 Schurt
5111 gold badge6 silver badges15 bronze badges

Using 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:

Community
kursancewkursancew

You can do this with psutil which is cross-platform:

evandrix
4,9813 gold badges23 silver badges31 bronze badges
Python NovicePython Novice
5193 gold badges10 silver badges25 bronze badges

Note 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 Fouhy
31.7k15 gold badges53 silver badges71 bronze badges

The 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).

Ghost of GoesGhost of Goes

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.

MostlyharmlessMostlyharmless

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 Lizard
304k159 gold badges506 silver badges799 bronze badges
Sarath Sadasivan PillaiSarath Sadasivan Pillai

Python Editor Notebook For Machine Learning Ipython

Ajay Kumar K KAjay Kumar K K

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.

DGentryDGentry
14.1k6 gold badges46 silver badges63 bronze badges

Best 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?

Not the answer you're looking for? Browse other questions tagged pythonwindowslinuxnetworking or ask your own question.