Python 获取 网卡 MAC 地址

/***********************************************************************
 *                    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()[1]
            print(mac)
            break

三、运行效果:
    [zengjf@root ~]#  ifconfig
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.203  netmask 255.255.254.0  broadcast 192.168.1.255
            ether 70:b3:d5:10:6f:90  txqueuelen 1000  (Ethernet)
            RX packets 3907  bytes 377873 (369.0 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 423  bytes 273550 (267.1 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 16436
            inet 127.0.0.1  netmask 255.0.0.0
            loop  txqueuelen 0  (Local Loopback)
            RX packets 48722  bytes 3461535 (3.3 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 48722  bytes 3461535 (3.3 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    [zengjf@root ~]#  python test.py 
    70:b3:d5:10:6f:90
    [zengjf@root ~]#  

 

posted on 2016-10-15 11:34  zengjf  阅读(3282)  评论(0编辑  收藏  举报

导航