V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
linuxzpf
V2EX  ›  Python

python 如何获取 Mac 地址?

  •  
  •   linuxzpf · 2015-01-17 21:13:14 +08:00 · 5960 次点击
    这是一个创建于 3389 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我用uuid模块取Mac地址的,刚开始很完美,可是后来发现同一台主机取来的Mac地址经常变化,很不靠谱啊,有时候取到的根本不是本机的Mac,也不知道为什么会这样。
    求助怎么用Python取一台主机的Mac并且不会自己乱变,另外我的代码贴上来:
    def getMac():
    mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
    return ":".join([mac[e:e+2] for e in range(0,11,2)])

    6 条回复    2019-06-11 09:21:43 +08:00
    fatbean
        1
    fatbean  
       2015-01-17 21:21:11 +08:00 via iPad
    你装了虚拟机就这样了
    linuxzpf
        2
    linuxzpf  
    OP
       2015-01-17 21:30:16 +08:00
    @fatbean 恰恰相反,我的vbox虚拟机取到的Mac都不会变,反而是取到本机的Mac变个不停,好烦
    s51431980
        3
    s51431980  
       2015-01-17 23:35:35 +08:00
    If you look at the getnode documentation it says that it will return a random long if it fails to detect the mac: "If all attempts to obtain the hardware address fail, we choose a random 48-bit number with its eighth bit set to 1 as recommended in RFC 4122."
    s51431980
        4
    s51431980  
       2015-01-17 23:36:08 +08:00
    linuxzpf
        5
    linuxzpf  
    OP
       2015-01-18 12:54:03 +08:00
    @s51431980 这些都是不行的哦亲,完美符合我说的变来变去的情况嘛。
    trueGate
        6
    trueGate  
       2019-06-11 09:21:43 +08:00
    @s51431980 感谢提醒,最近遇到几台电脑出现了文档中问题,导致返回了 48bit 随机数。请问您是否研究过在 windows 平台下修复该问题的办法呢?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2385 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:07 · PVG 00:07 · LAX 09:07 · JFK 12:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.