From 46fc6a08f62ed82fb67dd7625f97eb8c1bfdc22a Mon Sep 17 00:00:00 2001 From: Neal Norwitz Date: Fri, 28 Jul 2006 07:21:27 +0000 Subject: [PATCH] Try to find the MAC addr on various flavours of Unix. This seems hopeless. The reduces the test_uuid failures, but there's still another method failing. --- Lib/uuid.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index dabe22e5be4..b0afd945c10 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -271,22 +271,48 @@ class UUID(object): version = property(get_version) -def _ifconfig_getnode(): - """Get the hardware address on Unix by running ifconfig.""" +def _find_mac(command, args, hw_identifiers, get_index): import os for dir in ['', '/sbin/', '/usr/sbin']: try: # LC_ALL to get English output, 2>/dev/null to # prevent output on stderr - cmd = 'LC_ALL=C %s 2>/dev/null' % os.path.join(dir, 'ifconfig') + executable = os.path.join(dir, command) + cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args) pipe = os.popen(cmd) except IOError: continue + for line in pipe: words = line.lower().split() for i in range(len(words)): - if words[i] in ['hwaddr', 'ether']: - return int(words[i + 1].replace(':', ''), 16) + if words[i] in hw_identifiers: + return int(words[get_index(i)].replace(':', ''), 16) + return None + +def _ifconfig_getnode(): + """Get the hardware address on Unix by running ifconfig.""" + + # This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes. + for args in ('', '-a', '-av'): + mac = _find_mac('ifconfig', args, ['hwaddr', 'ether'], lambda i: i+1) + if mac: + return mac + + import socket + ip_addr = socket.gethostbyname(socket.gethostname()) + + # Try getting the MAC addr from arp based on our IP address (Solaris). + mac = _find_mac('arp', '-an', [ip_addr], lambda i: -1) + if mac: + return mac + + # This might work on HP-UX. + mac = _find_mac('lanscan', '-ai', ['lan0'], lambda i: 0) + if mac: + return mac + + return None def _ipconfig_getnode(): """Get the hardware address on Windows by running ipconfig.exe."""