Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63703 > unrolled thread
| Started by | Sam <lightaiyee@gmail.com> |
|---|---|
| First post | 2014-01-11 06:26 -0800 |
| Last post | 2014-01-14 03:47 +1100 |
| Articles | 15 — 9 participants |
Back to article view | Back to comp.lang.python
How to get Mac address of ethernet port? Sam <lightaiyee@gmail.com> - 2014-01-11 06:26 -0800
Re: How to get Mac address of ethernet port? Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2014-01-11 16:35 +0200
Re: How to get Mac address of ethernet port? "James Harris" <james.harris.1@gmail.com> - 2014-01-11 15:54 +0000
Re: How to get Mac address of ethernet port? Roy Smith <roy@panix.com> - 2014-01-11 11:29 -0500
Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-12 08:40 +1100
Re: How to get Mac address of ethernet port? Roy Smith <roy@panix.com> - 2014-01-11 16:55 -0500
Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-12 09:08 +1100
Re: How to get Mac address of ethernet port? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-01-12 11:22 -0500
Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-12 01:38 +1100
Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-12 01:52 +1100
Re: How to get Mac address of ethernet port? Skip Montanaro <skip@pobox.com> - 2014-01-11 09:03 -0600
Re: How to get Mac address of ethernet port? Michael Torrie <torriem@gmail.com> - 2014-01-11 09:34 -0700
Re: How to get Mac address of ethernet port? Sam <lightaiyee@gmail.com> - 2014-01-11 17:25 -0800
Re: How to get Mac address of ethernet port? William Ray Wing <wrw@mac.com> - 2014-01-13 10:42 -0500
Re: How to get Mac address of ethernet port? Chris Angelico <rosuav@gmail.com> - 2014-01-14 03:47 +1100
| From | Sam <lightaiyee@gmail.com> |
|---|---|
| Date | 2014-01-11 06:26 -0800 |
| Subject | How to get Mac address of ethernet port? |
| Message-ID | <6a5ceb3f-021d-4acc-b618-ce53530fa2dd@googlegroups.com> |
I would like to use python to retrieve the mac address of the ethernet port. Can this be done? Thank you.
[toc] | [next] | [standalone]
| From | Andriy Kornatskyy <andriy.kornatskyy@live.com> |
|---|---|
| Date | 2014-01-11 16:35 +0200 |
| Message-ID | <mailman.5329.1389450993.18130.python-list@python.org> |
| In reply to | #63703 |
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 <lightaiyee@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
[toc] | [prev] | [next] | [standalone]
| From | "James Harris" <james.harris.1@gmail.com> |
|---|---|
| Date | 2014-01-11 15:54 +0000 |
| Message-ID | <larpf5$3c1$1@dont-email.me> |
| In reply to | #63704 |
"Andriy Kornatskyy" <andriy.kornatskyy@live.com> wrote in message news:mailman.5329.1389450993.18130.python-list@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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-01-11 11:29 -0500 |
| Message-ID | <roy-B5B4DE.11290611012014@news.panix.com> |
| In reply to | #63710 |
In article <larpf5$3c1$1@dont-email.me>, "James Harris" <james.harris.1@gmail.com> wrote: > "Andriy Kornatskyy" <andriy.kornatskyy@live.com> wrote in message > news:mailman.5329.1389450993.18130.python-list@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>.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-12 08:40 +1100 |
| Message-ID | <mailman.5336.1389476410.18130.python-list@python.org> |
| In reply to | #63713 |
On Sun, Jan 12, 2014 at 3:29 AM, Roy Smith <roy@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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-01-11 16:55 -0500 |
| Message-ID | <roy-8189B7.16555311012014@news.panix.com> |
| In reply to | #63722 |
In article <mailman.5336.1389476410.18130.python-list@python.org>, Chris Angelico <rosuav@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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-12 09:08 +1100 |
| Message-ID | <mailman.5346.1389489827.18130.python-list@python.org> |
| In reply to | #63724 |
On Sun, Jan 12, 2014 at 8:55 AM, Roy Smith <roy@panix.com> wrote: > In article <mailman.5336.1389476410.18130.python-list@python.org>, > Chris Angelico <rosuav@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
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2014-01-12 11:22 -0500 |
| Message-ID | <mailman.5373.1389543774.18130.python-list@python.org> |
| In reply to | #63724 |
On Sat, 11 Jan 2014 16:55:53 -0500, Roy Smith <roy@panix.com> declaimed the
following:
>In article <mailman.5336.1389476410.18130.python-list@python.org>,
> Chris Angelico <rosuav@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.
>
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).
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-12 01:38 +1100 |
| Message-ID | <mailman.5330.1389451130.18130.python-list@python.org> |
| In reply to | #63703 |
On Sun, Jan 12, 2014 at 1:26 AM, Sam <lightaiyee@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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-12 01:52 +1100 |
| Message-ID | <mailman.5331.1389451941.18130.python-list@python.org> |
| In reply to | #63703 |
On Sun, Jan 12, 2014 at 1:35 AM, Andriy Kornatskyy
<andriy.kornatskyy@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
[toc] | [prev] | [next] | [standalone]
| From | Skip Montanaro <skip@pobox.com> |
|---|---|
| Date | 2014-01-11 09:03 -0600 |
| Message-ID | <mailman.5332.1389452592.18130.python-list@python.org> |
| In reply to | #63703 |
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
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2014-01-11 09:34 -0700 |
| Message-ID | <mailman.5333.1389458116.18130.python-list@python.org> |
| In reply to | #63703 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Sam <lightaiyee@gmail.com> |
|---|---|
| Date | 2014-01-11 17:25 -0800 |
| Message-ID | <0e20ca68-5643-40e6-adde-6bb929a4bb5d@googlegroups.com> |
| In reply to | #63716 |
On Sunday, January 12, 2014 12:34:35 AM UTC+8, Michael Torrie wrote: > 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!
[toc] | [prev] | [next] | [standalone]
| From | William Ray Wing <wrw@mac.com> |
|---|---|
| Date | 2014-01-13 10:42 -0500 |
| Message-ID | <mailman.5413.1389631361.18130.python-list@python.org> |
| In reply to | #63703 |
On Jan 11, 2014, at 11:34 AM, Michael Torrie <torriem@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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-14 03:47 +1100 |
| Message-ID | <mailman.5414.1389631648.18130.python-list@python.org> |
| In reply to | #63703 |
On Tue, Jan 14, 2014 at 2:42 AM, William Ray Wing <wrw@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web