Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get Mac address of ethernet port?

68 views
Skip to first unread message

Sam

unread,
Jan 11, 2014, 9:26:33 AM1/11/14
to
I would like to use python to retrieve the mac address of the ethernet port. Can this be done? Thank you.

Andriy Kornatskyy

unread,
Jan 11, 2014, 9:35:19 AM1/11/14
to Sam, pytho...@python.org
Sam,

How about this?

from uuid import getnode as get_mac
'%012x' % get_mac()

Thanks.

Andriy Kornatskyy

On Jan 11, 2014, at 4:26 PM, Sam <light...@gmail.com> wrote:

> I would like to use python to retrieve the mac address of the ethernet port. Can this be done? Thank you.
> --
> https://mail.python.org/mailman/listinfo/python-list

Chris Angelico

unread,
Jan 11, 2014, 9:38:40 AM1/11/14
to pytho...@python.org
On Sun, Jan 12, 2014 at 1:26 AM, Sam <light...@gmail.com> wrote:
> I would like to use python to retrieve the mac address of the ethernet port. Can this be done? Thank you.
>

Did you try searching the web for 'python retrieve mac address' or similar?

There are several options offered.

ChrisA

Chris Angelico

unread,
Jan 11, 2014, 9:52:12 AM1/11/14
to pytho...@python.org
On Sun, Jan 12, 2014 at 1:35 AM, Andriy Kornatskyy
<andriy.k...@live.com> wrote:
> from uuid import getnode as get_mac
> '%012x' % get_mac()
>

Code golf! Put colons in that, with as little code as possible.

# Way too verbose.
import uuid
l=list("%012x"%uuid.getnode())
l[10:10]=l[8:8]=l[6:6]=l[4:4]=l[2:2]=':'
mac = ''.join(l)

# Shorter but not short enough
import uuid
s="%012x"%uuid.getnode()
mac = ':'.join(s[i*2:i*2+2] for i in range(6))

:)

ChrisA

Skip Montanaro

unread,
Jan 11, 2014, 10:03:04 AM1/11/14
to Chris Angelico, pytho...@python.org
This is slightly longer than ChrisA's second solution:

>>> import uuid
>>> s = "%12x" % uuid.getnode()
>>> ":".join(x+y for x, y in zip(s[::2], s[1::2]))
'18:03:73:cb:2a:ee'

Skip

James Harris

unread,
Jan 11, 2014, 10:54:12 AM1/11/14
to
"Andriy Kornatskyy" <andriy.k...@live.com> wrote in message
news:mailman.5329.1389450...@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.

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.

James


Roy Smith

unread,
Jan 11, 2014, 11:29:06 AM1/11/14
to
In article <larpf5$3c1$1...@dont-email.me>,
"James Harris" <james.h...@gmail.com> wrote:

> "Andriy Kornatskyy" <andriy.k...@live.com> wrote in message
> news:mailman.5329.1389450...@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>.

Michael Torrie

unread,
Jan 11, 2014, 11:34:35 AM1/11/14
to pytho...@python.org
On 01/11/2014 07:35 AM, Andriy Kornatskyy wrote:
> Sam,
>
> How about this?
>
> from uuid import getnode as get_mac
> '%012x' % get_mac()

This seems to work if you have only one ethernet adapter. Most
computers have two (wired and wireless) adapters.

Getting a mac address is platform-specific, and the OP has not specified
what OS he is using.

On Windows I imagine you'd have to access the WMI subsystem in Windows.

On Linux you could access the /sys/devices/virtual/net/<interface name>
file in the sysfs filesystem. I'm sure there are other ways.

No idea on OS X.

Chris Angelico

unread,
Jan 11, 2014, 4:40:01 PM1/11/14
to pytho...@python.org
On Sun, Jan 12, 2014 at 3:29 AM, Roy Smith <r...@panix.com> wrote:
> 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>.

Most people shouldn't have to worry about MAC address
duplication/collision on the same subnet (I used MACs as a means of
guaranteeing uniqueness among a pool of application servers, for
instance), but MAC switching in software can occur in a typical home
internet connection scenario. We had a connection set up a few years
ago where the ISP tech recorded the source MAC into the far end, and
only that MAC would work - so when I stuck in a different router, I
needed to switch it to the old MAC before it could establish a
connection. Stupid? Yes. Unusual? I hope so, but still more likely
than coming across DECnet in a typical home!

ChrisA

Roy Smith

unread,
Jan 11, 2014, 4:55:53 PM1/11/14
to
In article <mailman.5336.1389476...@python.org>,
Chris Angelico <ros...@gmail.com> wrote:

> We had a connection set up a few years
> ago where the ISP tech recorded the source MAC into the far end, and
> only that MAC would work - so when I stuck in a different router, I
> needed to switch it to the old MAC before it could establish a
> connection. Stupid? Yes. Unusual? I hope so

Actually, I think it's pretty common.

I had exactly the same problem a few years ago. My DSL router fried
itself. I got a new one and it was easier to make it fake out the old
router's MAC than to get my carrier to update their head end
configuration[1].

[1] Roy's law of dealing with service providers. Anything you can do
yourself is easier than interfacing with tech support.

Chris Angelico

unread,
Jan 11, 2014, 5:08:20 PM1/11/14
to pytho...@python.org
On Sun, Jan 12, 2014 at 8:55 AM, Roy Smith <r...@panix.com> wrote:
> In article <mailman.5336.1389476...@python.org>,
> Chris Angelico <ros...@gmail.com> wrote:
>
>> We had a connection set up a few years
>> ago where the ISP tech recorded the source MAC into the far end, and
>> only that MAC would work - so when I stuck in a different router, I
>> needed to switch it to the old MAC before it could establish a
>> connection. Stupid? Yes. Unusual? I hope so
>
> Actually, I think it's pretty common.

Sad.

> I had exactly the same problem a few years ago. My DSL router fried
> itself. I got a new one and it was easier to make it fake out the old
> router's MAC than to get my carrier to update their head end
> configuration[1].
>
> [1] Roy's law of dealing with service providers. Anything you can do
> yourself is easier than interfacing with tech support.

Unless you expect that doing it yourself will take upwards of an hour,
don't even bother talking to tech support, at least with the ISPs I
know. There's only *ONE* time when I got results quicker than that
(or, say, half an hour absolute minimum): with iiNet, I rang their
support and got dropped into a classic IVR system, and one of the
options was "Press whatever to get the basic setup information for
your connection". A couple more prompts and I was given a prerecorded
pile of numbers and settings, one of which was the exact one I was
having trouble with. With any other kind of business, this sort of
thing belongs on the web site, but for obvious reasons that's less
useful for an ISP :)

ChrisA

Sam

unread,
Jan 11, 2014, 8:25:08 PM1/11/14
to
Thank you to everyone for the helpful answers. I am using Linux in this case. I think this is the direction I am looking for. Thanks!
Message has been deleted

William Ray Wing

unread,
Jan 13, 2014, 10:42:25 AM1/13/14
to pytho...@python.org, Michael Torrie, William Ray Wing
On Jan 11, 2014, at 11:34 AM, Michael Torrie <tor...@gmail.com> wrote:

> On 01/11/2014 07:35 AM, Andriy Kornatskyy wrote:
>> Sam,
>>
>> How about this?
>>
>> from uuid import getnode as get_mac
>> '%012x' % get_mac()
>
> This seems to work if you have only one ethernet adapter. Most
> computers have two (wired and wireless) adapters.
>
> Getting a mac address is platform-specific, and the OP has not specified
> what OS he is using.
>
> On Windows I imagine you'd have to access the WMI subsystem in Windows.
>
> On Linux you could access the /sys/devices/virtual/net/<interface name>
> file in the sysfs filesystem. I'm sure there are other ways.
>
> No idea on OS X.
> --
> https://mail.python.org/mailman/listinfo/python-list

There are probably several ways in OS-X, just as there are in any other UNIX system. The one I've used is to spawn a subprocess and run the "ifconfig" command with no arguments (which doesn't require any special privileges). This will return a string of all the network interfaces (including the loopback and firewire interfaces in addition to Ethernet and WiFi) and their config specs. The OP would then parse this string looking for the location of the phrase "status: active" and then back up to the mac address that precedes it. More work than using uuid, but this guarantees a current and correct answer.

>>> import string
>>> import subprocess
>>> mac_result = subprocess.Popen(['ifconfig'], stderr = subprocess.PIPE, stdout = subprocess.PIPE).communicate()[0]
>>> mac_loc = string.find(mac_result, "status: active")

...and so on.

Bill

Chris Angelico

unread,
Jan 13, 2014, 11:47:24 AM1/13/14
to pytho...@python.org
On Tue, Jan 14, 2014 at 2:42 AM, William Ray Wing <w...@mac.com> wrote:
> The one I've used is to spawn a subprocess and run the "ifconfig" command with no arguments (which doesn't require any special privileges).

Very small caveat: On some systems, running ifconfig doesn't require
privileges, but it's not in the unprivileged user's default path (it's
in root's path though). I've seen this on Debian Linux, for instance.
So you may need to explicitly call /sbin/ifconfig or whereever it's
stored.

ChrisA
0 new messages