@@ -304,8 +304,8 @@ def version(self):
304
304
if self .variant == RFC_4122 :
305
305
return int ((self .int >> 76 ) & 0xf )
306
306
307
- def _find_mac (command , args , hw_identifiers , get_index ):
308
- import os , shutil
307
+ def _find_mac (command , arg , hw_identifiers , get_index ):
308
+ import os , shutil , subprocess
309
309
executable = shutil .which (command )
310
310
if executable is None :
311
311
path = os .pathsep .join (('/sbin' , '/usr/sbin' ))
@@ -314,18 +314,26 @@ def _find_mac(command, args, hw_identifiers, get_index):
314
314
return None
315
315
316
316
try :
317
- # LC_ALL to ensure English output, 2>/dev/null to prevent output on
318
- # stderr (Note: we don't have an example where the words we search for
319
- # are actually localized, but in theory some system could do so.)
320
- cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable , args )
321
- with os .popen (cmd ) as pipe :
322
- for line in pipe :
317
+ # LC_ALL=C to ensure English output, stderr=DEVNULL to prevent output
318
+ # on stderr (Note: we don't have an example where the words we search
319
+ # for are actually localized, but in theory some system could do so.)
320
+ env = dict (os .environ )
321
+ env ['LC_ALL' ] = 'C'
322
+ cmd = [executable ]
323
+ if arg :
324
+ cmd .append (arg )
325
+ proc = subprocess .Popen (cmd ,
326
+ stdout = subprocess .PIPE ,
327
+ stderr = subprocess .DEVNULL ,
328
+ env = env )
329
+ with proc :
330
+ for line in proc .stdout :
323
331
words = line .lower ().split ()
324
332
for i in range (len (words )):
325
333
if words [i ] in hw_identifiers :
326
334
try :
327
335
return int (
328
- words [get_index (i )].replace (':' , '' ), 16 )
336
+ words [get_index (i )].replace (b ':' , b '' ), 16 )
329
337
except (ValueError , IndexError ):
330
338
# Virtual interfaces, such as those provided by
331
339
# VPNs, do not have a colon-delimited MAC address
@@ -341,20 +349,20 @@ def _ifconfig_getnode():
341
349
342
350
# This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
343
351
for args in ('' , '-a' , '-av' ):
344
- mac = _find_mac ('ifconfig' , args , ['hwaddr' , 'ether' ], lambda i : i + 1 )
352
+ mac = _find_mac ('ifconfig' , args , [b 'hwaddr' , b 'ether' ], lambda i : i + 1 )
345
353
if mac :
346
354
return mac
347
355
348
356
import socket
349
357
ip_addr = socket .gethostbyname (socket .gethostname ())
350
358
351
359
# Try getting the MAC addr from arp based on our IP address (Solaris).
352
- mac = _find_mac ('arp' , '-an' , [ip_addr ], lambda i : - 1 )
360
+ mac = _find_mac ('arp' , '-an' , [os . fsencode ( ip_addr ) ], lambda i : - 1 )
353
361
if mac :
354
362
return mac
355
363
356
364
# This might work on HP-UX.
357
- mac = _find_mac ('lanscan' , '-ai' , ['lan0' ], lambda i : 0 )
365
+ mac = _find_mac ('lanscan' , '-ai' , [b 'lan0' ], lambda i : 0 )
358
366
if mac :
359
367
return mac
360
368
0 commit comments