summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2006-07-28 07:21:27 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2006-07-28 07:21:27 (GMT)
commit46fc6a08f62ed82fb67dd7625f97eb8c1bfdc22a (patch)
tree6f44f99c5d5c43750c303c71dce7139132829ab8 /Lib
parent750c4420a86852bf549ded572c52b544b7c4bc6b (diff)
downloadcpython-46fc6a08f62ed82fb67dd7625f97eb8c1bfdc22a.zip
cpython-46fc6a08f62ed82fb67dd7625f97eb8c1bfdc22a.tar.gz
cpython-46fc6a08f62ed82fb67dd7625f97eb8c1bfdc22a.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/uuid.py36
1 files changed, 31 insertions, 5 deletions
diff --git a/Lib/uuid.py b/Lib/uuid.py
index dabe22e..b0afd94 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."""