@@ -304,8 +304,8 @@ def version(self):
304304 if self .variant == RFC_4122 :
305305 return int ((self .int >> 76 ) & 0xf )
306306
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
309309 executable = shutil .which (command )
310310 if executable is None :
311311 path = os .pathsep .join (('/sbin' , '/usr/sbin' ))
@@ -314,18 +314,26 @@ def _find_mac(command, args, hw_identifiers, get_index):
314314 return None
315315
316316 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 :
323331 words = line .lower ().split ()
324332 for i in range (len (words )):
325333 if words [i ] in hw_identifiers :
326334 try :
327335 return int (
328- words [get_index (i )].replace (':' , '' ), 16 )
336+ words [get_index (i )].replace (b ':' , b '' ), 16 )
329337 except (ValueError , IndexError ):
330338 # Virtual interfaces, such as those provided by
331339 # VPNs, do not have a colon-delimited MAC address
@@ -341,20 +349,20 @@ def _ifconfig_getnode():
341349
342350 # This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
343351 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 )
345353 if mac :
346354 return mac
347355
348356 import socket
349357 ip_addr = socket .gethostbyname (socket .gethostname ())
350358
351359 # 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 )
353361 if mac :
354362 return mac
355363
356364 # 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 )
358366 if mac :
359367 return mac
360368
0 commit comments