python 如何获取 Mac 地址?

2015-01-17 21:13:14 +08:00
 linuxzpf

我用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)])

5973 次点击
所在节点    Python
6 条回复
fatbean
2015-01-17 21:21:11 +08:00
你装了虚拟机就这样了
linuxzpf
2015-01-17 21:30:16 +08:00
@fatbean 恰恰相反,我的vbox虚拟机取到的Mac都不会变,反而是取到本机的Mac变个不停,好烦
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
2015-01-17 23:36:08 +08:00
linuxzpf
2015-01-18 12:54:03 +08:00
@s51431980 这些都是不行的哦亲,完美符合我说的变来变去的情况嘛。
trueGate
2019-06-11 09:21:43 +08:00
@s51431980 感谢提醒,最近遇到几台电脑出现了文档中问题,导致返回了 48bit 随机数。请问您是否研究过在 windows 平台下修复该问题的办法呢?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/163061

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX