Python 获取 网卡 MAC 地址

本文介绍了一种使用Python获取本地计算机网卡MAC地址的方法。通过执行特定的shell命令并解析输出,可以有效地抓取到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 ~]#  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值