获取MAC地址 [英] Getting MAC Address
问题描述
我需要一种在运行时确定计算机的MAC地址的跨平台方法.对于Windows,可以使用"wmi"模块,在Linux下,我能找到的唯一方法是运行ifconfig并在其输出中运行一个正则表达式.我不喜欢使用仅在一个OS上可用的程序包,而且更不用说容易出错的语法解析另一个程序的输出.
I need a cross platform method of determining the MAC address of a computer at run time. For windows the 'wmi' module can be used and the only method under Linux I could find was to run ifconfig and run a regex across its output. I don't like using a package that only works on one OS, and parsing the output of another program doesn't seem very elegant not to mention error prone.
有人知道跨平台方法(Windows和Linux)方法来获取MAC地址吗?如果没有,还有谁比我上面列出的方法更优雅?
Does anyone know a cross platform method (windows and linux) method to get the MAC address? If not, does anyone know any more elegant methods then those I listed above?
推荐答案
Python 2.5包含一个uuid实现,该实现(至少在一个版本中)需要mac地址.您可以轻松地将mac查找功能导入您自己的代码中:
Python 2.5 includes an uuid implementation which (in at least one version) needs the mac address. You can import the mac finding function into your own code easily:
from uuid import getnode as get_mac
mac = get_mac()
返回值是作为48位整数的mac地址.
The return value is the mac address as 48 bit integer.
这篇关于获取MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!