Skip to content

Commit ec2b2dc

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
bpo-9678: Fix determining the MAC address in the uuid module. (GH-4264) (#4269)
* Using ifconfig on NetBSD and OpenBSD. * Using arp on Linux, FreeBSD, NetBSD and OpenBSD. Based on patch by Takayuki Shimizukawa. (cherry picked from commit ee1a9a2)
1 parent 8ce9854 commit ec2b2dc

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

Lib/uuid.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ def _find_mac(command, args, hw_identifiers, get_index):
349349
def _ifconfig_getnode():
350350
"""Get the hardware address on Unix by running ifconfig."""
351351
# This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
352+
keywords = (b'hwaddr', b'ether', b'address:', b'lladdr')
352353
for args in ('', '-a', '-av'):
353-
mac = _find_mac('ifconfig', args, [b'hwaddr', b'ether'], lambda i: i+1)
354+
mac = _find_mac('ifconfig', args, keywords, lambda i: i+1)
354355
if mac:
355356
return mac
356357

@@ -370,7 +371,20 @@ def _arp_getnode():
370371
return None
371372

372373
# Try getting the MAC addr from arp based on our IP address (Solaris).
373-
return _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
374+
mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
375+
if mac:
376+
return mac
377+
378+
# This works on OpenBSD
379+
mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: i+1)
380+
if mac:
381+
return mac
382+
383+
# This works on Linux, FreeBSD and NetBSD
384+
mac = _find_mac('arp', '-an', [os.fsencode('(%s)' % ip_addr)],
385+
lambda i: i+2)
386+
if mac:
387+
return mac
374388

375389
def _lanscan_getnode():
376390
"""Get the hardware address on Unix by running lanscan."""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fixed determining the MAC address in the uuid module:
2+
3+
* Using ifconfig on NetBSD and OpenBSD.
4+
* Using arp on Linux, FreeBSD, NetBSD and OpenBSD.
5+
6+
Based on patch by Takayuki Shimizukawa.

0 commit comments

Comments
 (0)