python(5) 读取mac地址
时间:2009-03-14 来源:alexnetwork
#!/usr/bin/python
import os
import re
CmdLine = 'arp -n'
r = os.popen(CmdLine).read()
L = re.findall('10.26.12.1.*?([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)
ret = L[0]
print ret
*****************************
import os
import re
def GetMac():
#Windows下的
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
#Linux/Unix下
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 = GetMac()
print mac
import os
import re
CmdLine = 'arp -n'
r = os.popen(CmdLine).read()
L = re.findall('10.26.12.1.*?([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)
ret = L[0]
print ret
*****************************
import os
import re
def GetMac():
#Windows下的
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
#Linux/Unix下
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 = GetMac()
print mac
相关阅读 更多 +