How to get Mac address of ethernet port?

S

Sam

I would like to use python to retrieve the mac address of the ethernet port. Can this be done? Thank you.
 
A

Andriy Kornatskyy

Sam,

How about this?

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

Thanks.

Andriy Kornatskyy
 
C

Chris Angelico

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
 
J

James Harris

Andriy Kornatskyy said:
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
 
R

Roy Smith

James Harris said:
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>.
 
M

Michael Torrie

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.
 
C

Chris Angelico

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
 
R

Roy Smith

Chris Angelico said:
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.
 
C

Chris Angelico

Chris Angelico said:
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
 
S

Sam

On 01/11/2014 07:35 AM, Andriy Kornatskyy wrote:


On Linux you could access the /sys/devices/virtual/net/<interface name>

file in the sysfs filesystem. I'm sure there are other ways.

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!
 
D

Dennis Lee Bieber

Actually, I think it's pretty common.

In my situation, the first thing I did with the router was "MAC address
clone" of my desktop computer... Why? My ISP wanted [still does, I think]
to charge $10/month for the privilege of a LAN vs direct single connection
(worse, for the same router I already had owned).

This way, if I did encounter problems, I would be able to bypass the
router without appearing as a different NIC to the DSL adapter (granted, I
might have had a different problem, as the adapter was in bridge mode and I
never installed the PPPoE software on my desktop; my router handled that).
 
C

Chris Angelico

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top