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 ~]#  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM