pythonでmac物理アドレスを取得する

方法1
>>> import uuid
>>> node = uuid.getnode()
>>> mac = uuid.UUID(int=node)
>>> addr = mac.hex[-12:]
>>> addr

方法2
#!/bin/python

import os
import re

def GetMacAddressFun():
if os.name == 'nt’:
try:
ret = "
CmdLine = 'ipconfig /all’
r = os.popen(CmdLine).read()
if r:
L = re.findall('Physical Address.*?([0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2})’, r)
if len(L) > 0:
ret = L[0]
except:
pass

elif os.name == “posix":
try:
ret = "
CmdLine = 'ifconfig’
r = os.popen(CmdLine).read()
if r:
L = re.findall('HWaddr.*?([0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2})’, r)
if len(L) > 0:
ret = L[0]
except:
pass
else:
pass
return ret

if __name__ == '__main__’:
mac = GetMacAddressFun()
print mac
m=raw_input()

Python

Posted by arkgame