Welcome, guest | Sign In | My Account | Store | Cart

uuid.getnode represents current mac address as an integer, this one-liner formats this number in a standard mac adress form (i.e. bytes splitted by :)

Python, 2 lines
1
2
import uuid
print ':'.join(['{:02x}'.format((uuid.getnode() >> i) & 0xff) for i in range(0,8*6,8)][::-1])

4 comments

Uri Yanover 11 years, 5 months ago  # | flag

This would appear as a bad idea, for the following reasons:

  1. The value returned may turn out to be not useful in the intended sense - it may as return unintended values e.g., the MAC address of a Bluetooth interface or even virtual interfaces ("Microsoft ISATAP Adapter"),
  2. If a computer has no interfaces with MAC address, uuid.getnode() will return a random return value), which will change from run to run.
  3. If a computer has several interfaces with MAC address, it may still return inconsistent values between runs (e.g., due to initialization of virtual interfaces in different orders)
  4. For almost any need involving MAC addresses, a scenario exists wherein the change of the address at some point in time will cause breakage to the application.

If I may suggest advise to an engineer that needs to fetch the MAC address, I'd advise him to:

  1. If he/she needs a transient unique value, I'd suggest generating it using uuid.
  2. If he/she needs a stationary unique value, I'd suggest generating it once during application deployment using uuid and storing it as part of the application data.
  3. If he/she needs a computer fingerprint, it is a much more complicated, platform-dependent endeavour; CPUID is probably a decent way of doing it on x86 and there's a library for that;
  4. Finally, if a programmer needs to get MAC addresses for all of the interfaces of the computer, it asks for a library that would take care of the problems above by describing the problem domain (e.g., a set of Layer2Interface each with an Address property); such a library would necessarily be at least several hundred LOCs big and be platform dependent; if you (the reader) happen to be implementing it, I'd implore you to publish it open-source. (I'm considering doing it now, but I have no concrete needs for it, and I'd need to have at least one specific use case in front of me to know I'm creating something usable).
Leonid Vasilyev (author) 11 years, 5 months ago  # | flag

This code is not intended for production use, nor it is intended to work across all the variety of available platforms. What I'm showing here is just a one-line hack, which I found useful in some cases.

Oren Tirosh 11 years, 5 months ago  # | flag

Splitting a string to chunks of some fixed length is easy and readable using the re module:

import re, uuid
print ':'.join(re.findall('..', '%012x' % uuid.getnode()))
Leonid Vasilyev (author) 11 years, 4 months ago  # | flag

Wow, this is cool I didn't knew that. Thanks Oren!

Created by Leonid Vasilyev on Mon, 1 Oct 2012 (MIT)
Python recipes (4591)
Leonid Vasilyev's recipes (1)

Required Modules

Other Information and Tasks