python脚本获取主机Mac地址

#!/usr/bin/python
import re
import subprocess
ARP = "arp"
IP = "192.168.128.27"
CMD = "%s %s" % (ARP,IP)
macPattern = re.compile(":")
def getMac():
p = subprocess.Popen(CMD,shell=True,stdout=subprocess.PIPE)
out = p.stdout.read()
results = out.split()
for chunk in results:
if re.search(macPattern,chunk):
return chunk
if __name__ == "__main__":
macAddr = getMac()
print macAddr

 

posted @ 2017-11-03 15:54  西门运维  阅读(1304)  评论(0编辑  收藏  举报