Skip to content

Commit ee1a9a2

Browse files
bpo-9678: Fix determining the MAC address in the uuid module. (#4264)
* Using ifconfig on NetBSD and OpenBSD. * Using arp on Linux, FreeBSD, NetBSD and OpenBSD. Based on patch by Takayuki Shimizukawa.
1 parent 93952f8 commit ee1a9a2

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

‎Lib/uuid.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,9 @@ def _find_mac(command, args, hw_identifiers, get_index):
370370
def _ifconfig_getnode():
371371
"""Get the hardware address on Unix by running ifconfig."""
372372
# This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
373+
keywords = (b'hwaddr', b'ether', b'address:', b'lladdr')
373374
for args in ('', '-a', '-av'):
374-
mac = _find_mac('ifconfig', args, [b'hwaddr', b'ether'], lambda i: i+1)
375+
mac = _find_mac('ifconfig', args, keywords, lambda i: i+1)
375376
if mac:
376377
return mac
377378

@@ -391,7 +392,20 @@ def _arp_getnode():
391392
return None
392393

393394
# Try getting the MAC addr from arp based on our IP address (Solaris).
394-
return _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
395+
mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
396+
if mac:
397+
return mac
398+
399+
# This works on OpenBSD
400+
mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: i+1)
401+
if mac:
402+
return mac
403+
404+
# This works on Linux, FreeBSD and NetBSD
405+
mac = _find_mac('arp', '-an', [os.fsencode('(%s)' % ip_addr)],
406+
lambda i: i+2)
407+
if mac:
408+
return mac
395409

396410
def _lanscan_getnode():
397411
"""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)