How to get Mac address of ethernet port?

Roy Smith roy at panix.com
Sat Jan 11 11:29:06 EST 2014


In article <larpf5$3c1$1 at dont-email.me>,
 "James Harris" <james.harris.1 at gmail.com> wrote:

> "Andriy Kornatskyy" <andriy.kornatskyy at live.com> wrote in message 
> news:mailman.5329.1389450993.18130.python-list at python.org...
> > Sam,
> >
> > How about this?
> >
> > from uuid import getnode as get_mac
> > '%012x' % get_mac()
> 
> AIUI that will return a mac address even if there isn't one. That may or may 
> not suit the OP.

Specifically, it says, "If all attempts to obtain the hardware address 
fail, we choose a random 48-bit number with its eighth bit set to 1 as 
recommended in RFC 4122".  Keep in mind that 4122 is all about 
generating globally unique strings.  The only reason it even talks about 
MAC addresses is in the context of one possible way to generate uuids.

If your goal is to get the MAC address for some sort of networking 
reason, you need to bear in mind what James says below:

> To the OP, depending on what you want to do remember that a machine can have 
> more than one mac address and that a mac address can differ from the 
> burned-in address (BIA) as some cards allow the effective mac address to be 
> changed in software. So it's possible that two machines could show the same 
> mac address.

If you don't believe that two machines can have the same MAC address, 
look up Hot Standby Router Protocol.  And if you don't believe a machine 
can ignore the BIA and assign a new MAC address in software, look up 
Decnet <insert derogatory gesture with sound effect here>.



More information about the Python-list mailing list