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 :)
1 2 | import uuid
print ':'.join(['{:02x}'.format((uuid.getnode() >> i) & 0xff) for i in range(0,8*6,8)][::-1])
|
Tags: networking, oneliner
This would appear as a bad idea, for the following reasons:
If I may suggest advise to an engineer that needs to fetch the MAC address, I'd advise him to:
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.
Splitting a string to chunks of some fixed length is easy and readable using the re module:
Wow, this is cool I didn't knew that. Thanks Oren!