zoukankan      html  css  js  c++  java
  • python 获取 mac 地址 的代码

    python 获取 mac 地址 的例子,有需要的朋友可以参考下。
    #!/bin/python
    import os
    import re
    def GetMac():
        if os.name == 'nt':
            try:
                ret = ''
                CmdLine = 'ipconfig /all'
                r = os.popen(CmdLine).read()
                if r:
                    L = re.findall('Physical Address.*?([0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2})', r)
                    if len(L) > 0:
                        ret = L[0]
            except:
                pass
            
        elif os.name == "posix":
            try:
                ret = ''
                CmdLine = 'ifconfig'
                r = os.popen(CmdLine).read()
                if r:
                    L = re.findall('HWaddr.*?([0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2})', r)
                    if len(L) > 0:
                        ret = L[0]
            except:
                pass
        else:
            pass
        return ret
    if __name__ == '__main__':
        mac = GetMac()
        print mac
        m=raw_input()

    一个简单的方法

    >>> import uuid
    >>> node = uuid.getnode()
    >>> mac = uuid.UUID(int=node)
    >>> addr = mac.hex[-12:]
    >>> addr
    更多有关python的内容,可以参考python 教程系列文章。
  • 相关阅读:
    【C++ STL】List
    【C++ STL】Deques
    【C++ STL】Vector
    【C++ STL】容器概要
    linux shell读取配置文件
    【C++对象模型】第六章 执行期语意学
    【C++对象模型】第五章 构造、解构、拷贝 语意学
    【C++对象模型】第四章 Function 语意学
    【C++对象模型】第三章 Data语义学
    [翻译]解读CSS中的长度单位
  • 原文地址:https://www.cnblogs.com/linuxnotes/p/3488440.html
Copyright © 2011-2022 走看看