4

How can I get my mac address in python in the form of: \x##\x##\x##\x##\x##\x##?
I'm trying to construct an ethernet header and generate the source hardware address automatically.

I'm using uuid.getnode() to get the mac address as a 48 bit integer. How can I convert it so I can append it to a string already in that format?
ie: eth_header = "\xff\xff\xff\xff\xff\xff" + magicconvert(uuid.getnode())

3 Answers 3

3

The user nneonneo answered this on September 13th. Credit goes to him:

    '-'.join('%02X' % ((uuid.getnode() >> 8*i) & 0xff) for i in reversed(xrange(6)))

Edit this to give you the string in formatted how you'd like.

I'd also like to take this moment to say that "uuid.getnode()" may very well be returning a random value, or it may be getting the MAC address of any random network interface on your system. From what I can tell, there really is no way to know.

Sign up to request clarification or add additional context in comments.

Comments

2

I'm sure there are many ways, how about this one:

import uuid
hdr="\xff"*6
mac=uuid.getnode()
txt="%012X"%mac
as_b=[int("".join(x),16) for x in map(None,*(txt[::2],txt[1::2]))]
as_s="".join(chr(b) for b in as_b)
out=hdr+as_s

Comments

0
from itertools import izip
import uuid

token = "\\x"
mac_address = "%012x" % uuid.getnode()
iter_mac_address = iter(mac_address)
"%s%s" % (token, token.join(a+b for a, b in izip(iter_mac_address, iter_mac_address)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.