Python 获取 网卡 MAC 地址

时间:2022-01-24 09:50:59
/***********************************************************************
* Python 获取 网卡 MAC 地址
* 说明:
* 记录一下Python如何获取网卡MAC地址,主要用于数据唯一性保存。
*
* 2016-10-15 深圳 南山平山村 曾剑锋
**********************************************************************/ 一、参考文档:
python 获取mac地址
http://www.cnblogs.com/Jerryshome/archive/2011/11/30/2269365.html 二、测试代码:
import os for line in os.popen("/sbin/ifconfig"):
if 'ether' in line:
mac = line.split()[]
print(mac)
break 三、运行效果:
[zengjf@root ~]# ifconfig
eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.1.203 netmask 255.255.254.0 broadcast 192.168.1.255
ether :b3:d5::6f: txqueuelen (Ethernet)
RX packets bytes (369.0 KiB)
RX errors dropped overruns frame
TX packets bytes (267.1 KiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen (Local Loopback)
RX packets bytes (3.3 MiB)
RX errors dropped overruns frame
TX packets bytes (3.3 MiB)
TX errors dropped overruns carrier collisions [zengjf@root ~]# python test.py
:b3:d5::6f:
[zengjf@root ~]#